smb1398-charger.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "SMB1398: %s: " fmt, __func__
  7. #include <linux/device.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pmic-voter.h>
  15. #include <linux/power_supply.h>
  16. #include <linux/regmap.h>
  17. #include <linux/iio/consumer.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/qti_power_supply.h>
  20. #include <dt-bindings/iio/qti_power_supply_iio.h>
  21. /* Status register definition */
  22. #define REVID_REVISION4 0x103
  23. #define INPUT_STATUS_REG 0x2609
  24. #define INPUT_USB_IN BIT(1)
  25. #define INPUT_WLS_IN BIT(0)
  26. #define PERPH0_INT_RT_STS_REG 0x2610
  27. #define USB_IN_OVLO_STS BIT(7)
  28. #define WLS_IN_OVLO_STS BIT(6)
  29. #define USB_IN_UVLO_STS BIT(5)
  30. #define WLS_IN_UVLO_STS BIT(4)
  31. #define DIV2_IREV_LATCH_STS BIT(3)
  32. #define VOL_UV_LATCH_STS BIT(2)
  33. #define TEMP_SHUTDOWN_STS BIT(1)
  34. #define CFLY_HARD_FAULT_LATCH_STS BIT(0)
  35. #define MODE_STATUS_REG 0x2641
  36. #define SMB_EN BIT(7)
  37. #define PRE_EN_DCDC BIT(6)
  38. #define DIV2_EN_SLAVE BIT(5)
  39. #define LCM_EN BIT(4)
  40. #define DIV2_EN BIT(3)
  41. #define BUCK_EN BIT(2)
  42. #define CFLY_SS_DONE BIT(1)
  43. #define DCDC_EN BIT(0)
  44. #define SWITCHER_OFF_WIN_STATUS_REG 0x2642
  45. #define DIV2_WIN_OV BIT(1)
  46. #define DIV2_WIN_UV BIT(0)
  47. #define SWITCHER_OFF_VIN_STATUS_REG 0x2643
  48. #define USB_IN_OVLO BIT(3)
  49. #define WLS_IN_OVLO BIT(2)
  50. #define USB_IN_UVLO BIT(1)
  51. #define WLS_IN_UVLO BIT(0)
  52. #define SWITCHER_OFF_FAULT_REG 0x2644
  53. #define VOUT_OV_3LVL_BUCK BIT(5)
  54. #define VOUT_UV_LATCH BIT(4)
  55. #define ITERM_3LVL_LATCH BIT(3)
  56. #define DIV2_IREV_LATCH BIT(2)
  57. #define TEMP_SHDWN BIT(1)
  58. #define CFLY_HARD_FAULT_LATCH BIT(0)
  59. #define BUCK_CC_CV_STATE_REG 0x2645
  60. #define BUCK_IN_CC_REGULATION BIT(1)
  61. #define BUCK_IN_CV_REGULATION BIT(0)
  62. #define INPUT_CURRENT_REGULATION_REG 0x2646
  63. #define BUCK_IN_ICL BIT(1)
  64. #define DIV2_IN_ILIM BIT(0)
  65. /* Config register definition */
  66. #define PERPH0_MISC_CFG2_REG 0x2636
  67. #define CFG_TEMP_PIN_ITEMP BIT(1)
  68. #define MISC_USB_WLS_SUSPEND_REG 0x2630
  69. #define WLS_SUSPEND BIT(1)
  70. #define USB_SUSPEND BIT(0)
  71. #define MISC_SL_SWITCH_EN_REG 0x2631
  72. #define EN_SLAVE BIT(1)
  73. #define EN_SWITCHER BIT(0)
  74. #define MISC_DIV2_3LVL_CTRL_REG 0x2632
  75. #define MISC_DIV2_3LVL_CTRL_MASK GENMASK(7, 0)
  76. #define EN_DIV2_CP BIT(2)
  77. #define EN_3LVL_BULK BIT(1)
  78. #define EN_CHG_2X BIT(0)
  79. #define MISC_CFG0_REG 0x2634
  80. #define DIS_SYNC_DRV_BIT BIT(5)
  81. #define SW_EN_SWITCHER_BIT BIT(3)
  82. #define CFG_DIS_FPF_IREV_BIT BIT(1)
  83. #define MISC_CFG1_REG 0x2635
  84. #define MISC_CFG1_MASK GENMASK(7, 0)
  85. #define CFG_OP_MODE_MASK GENMASK(2, 0)
  86. #define OP_MODE_DISABLED 0
  87. #define OP_MODE_3LVL_BULK 1
  88. #define OP_MODE_COMBO 2
  89. #define OP_MODE_DIV2_CP 3
  90. #define OP_MODE_PRE_REG_3S 4
  91. #define OP_MODE_ITLGS_1P 5
  92. #define OP_MODE_ITLGS_2X 6
  93. #define OP_MODE_PRE_REGULATOR 7
  94. #define MISC_CFG2_REG 0x2636
  95. #define NOLOCK_SPARE_REG 0x2637
  96. #define EN_SLAVE_OWN_FREQ_BIT BIT(5)
  97. #define DIV2_WIN_UV_SEL_BIT BIT(4)
  98. #define DIV2_WIN_UV_25MV 0
  99. #define COMBO_WIN_LO_EXIT_SEL_MASK GENMASK(3, 2)
  100. #define EXIT_DIV2_VOUT_HI_12P5MV 0
  101. #define EXIT_DIV2_VOUT_HI_25MV 1
  102. #define EXIT_DIV2_VOUT_HI_50MV 2
  103. #define EXIT_DIV2_VOUT_HI_75MV 3
  104. #define COMBO_WIN_HI_EXIT_SEL_MASK GENMASK(1, 0)
  105. #define EXIT_DIV2_VOUT_LO_75MV 0
  106. #define EXIT_DIV2_VOUT_LO_100MV 1
  107. #define EXIT_DIV2_VOUT_LO_200MV 2
  108. #define EXIT_DIV2_VOUT_LO_250MV 3
  109. #define SMB_EN_TRIGGER_CFG_REG 0x2639
  110. #define SMB_EN_NEG_TRIGGER BIT(1)
  111. #define SMB_EN_POS_TRIGGER BIT(0)
  112. #define PERPH0_DIV2_SLAVE 0x2652
  113. #define CFG_EN_SLAVE_OWN_FREQ BIT(1)
  114. #define CFG_DIV2_SYNC_CLK_PHASE_90 BIT(0)
  115. #define DIV2_LCM_CFG_REG 0x2653
  116. #define DIV2_LCM_REFRESH_TIMER_SEL_MASK GENMASK(5, 4)
  117. #define DIV2_WIN_BURST_HIGH_REF_MASK GENMASK(3, 2)
  118. #define DIV2_WIN_BURST_LOW_REF_MASK GENMASK(1, 0)
  119. #define DIV2_CURRENT_REG 0x2655
  120. #define DIV2_EN_ILIM_DET BIT(2)
  121. #define DIV2_EN_IREV_DET BIT(1)
  122. #define DIV2_EN_OCP_DET BIT(0)
  123. #define DIV2_PROTECTION_REG 0x2656
  124. #define DIV2_WIN_OV_SEL_MASK GENMASK(1, 0)
  125. #define WIN_OV_200_MV 0
  126. #define WIN_OV_300_MV 1
  127. #define WIN_OV_400_MV 2
  128. #define WIN_OV_500_MV 3
  129. #define PERPH0_OVLO_REF_REG 0x265B
  130. #define SMB1394_INPUT_OVLO_CONF_MASK GENMASK(2, 0)
  131. #define SMB1394_INPUT_OVLO_13P04V 0x5
  132. #define DIV2_MODE_CFG_REG 0x265C
  133. #define LCM_EXIT_CTRL_REG 0x265D
  134. #define ICHG_SS_DAC_TARGET_REG 0x2660
  135. #define ICHG_SS_DAC_VALUE_MASK GENMASK(5, 0)
  136. #define ICHG_STEP_MA 100
  137. #define VOUT_DAC_TARGET_REG 0x2663
  138. #define VOUT_DAC_VALUE_MASK GENMASK(7, 0)
  139. #define VOUT_1P_MIN_MV 3300
  140. #define VOUT_1S_MIN_MV 6600
  141. #define VOUT_1P_STEP_MV 10
  142. #define VOUT_1S_STEP_MV 20
  143. #define VOUT_SS_DAC_TARGET_REG 0x2666
  144. #define VOUT_SS_DAC_VALUE_MASK GENMASK(5, 0)
  145. #define VOUT_SS_1P_STEP_MV 90
  146. #define VOUT_SS_1S_STEP_MV 180
  147. #define IIN_SS_DAC_TARGET_REG 0x2669
  148. #define IIN_SS_DAC_VALUE_MASK GENMASK(6, 0)
  149. #define IIN_STEP_MA 50
  150. #define PERPH0_DIV2_REF_CFG 0x2671
  151. #define CFG_IREV_REF_BIT BIT(2)
  152. #define PERPH0_CFG_SDCDC_REG 0x267A
  153. #define EN_WIN_UV_BIT BIT(7)
  154. #define EN_WIN_OV_RISE_DEB_BIT BIT(6)
  155. #define PERPH0_SOVP_CFG0_REG 0x2680
  156. #define CFG_OVP_VSNS_THRESHOLD BIT(4)
  157. #define CFG_OVP_IGNORE_UVLO BIT(5)
  158. #define PERPH0_SSUPPLY_CFG0_REG 0x2682
  159. #define EN_HV_OV_OPTION2_BIT BIT(7)
  160. #define EN_MV_OV_OPTION2_BIT BIT(5)
  161. #define CFG_CMP_VOUT_VS_4V_REF_MASK GENMASK(2, 1)
  162. #define CMP_VOUT_VS_4V_REF_3P2V 0x3 /* Value for SMB1394 only */
  163. #define SSUPLY_TEMP_CTRL_REG 0x2683
  164. #define SEL_OUT_TEMP_MAX_MASK GENMASK(7, 5)
  165. #define SEL_OUT_TEMP_MAX_SHFT 5
  166. #define SEL_OUT_HIGHZ (0 << SEL_OUT_TEMP_MAX_SHFT)
  167. #define SEL_OUT_VTEMP (1 << SEL_OUT_TEMP_MAX_SHFT)
  168. #define SEL_OUT_ICHG (2 << SEL_OUT_TEMP_MAX_SHFT)
  169. #define SEL_OUT_IIN_FB (4 << SEL_OUT_TEMP_MAX_SHFT)
  170. #define PERPH1_INT_RT_STS_REG 0x2710
  171. #define DIV2_WIN_OV_STS BIT(7)
  172. #define DIV2_WIN_UV_STS BIT(6)
  173. #define DIV2_ILIM_STS BIT(5)
  174. #define DIV2_CFLY_SS_DONE_STS BIT(1)
  175. #define PERPH1_LOCK_SPARE_REG 0x27C3
  176. #define CFG_LOCK_SPARE1_MASK GENMASK(7, 6)
  177. #define CFG_LOCK_SPARE1_SHIFT 6
  178. /* available voters */
  179. #define ILIM_VOTER "ILIM_VOTER"
  180. #define TAPER_VOTER "TAPER_VOTER"
  181. #define STATUS_CHANGE_VOTER "STATUS_CHANGE_VOTER"
  182. #define SHUTDOWN_VOTER "SHUTDOWN_VOTER"
  183. #define CUTOFF_SOC_VOTER "CUTOFF_SOC_VOTER"
  184. #define SRC_VOTER "SRC_VOTER"
  185. #define ICL_VOTER "ICL_VOTER"
  186. #define WIRELESS_VOTER "WIRELESS_VOTER"
  187. #define SWITCHER_TOGGLE_VOTER "SWITCHER_TOGGLE_VOTER"
  188. #define USER_VOTER "USER_VOTER"
  189. #define FCC_VOTER "FCC_VOTER"
  190. #define CP_VOTER "CP_VOTER"
  191. #define CC_MODE_VOTER "CC_MODE_VOTER"
  192. #define MAIN_DISABLE_VOTER "MAIN_DISABLE_VOTER"
  193. #define TAPER_MAIN_ICL_LIMIT_VOTER "TAPER_MAIN_ICL_LIMIT_VOTER"
  194. /* Constant definitions */
  195. #define DIV2_MAX_ILIM_UA 5000000
  196. #define DIV2_MAX_ILIM_DUAL_CP_UA 10000000
  197. #define DIV2_ILIM_CFG_PCT 105
  198. #define TAPER_STEPPER_UA_DEFAULT 100000
  199. #define TAPER_STEPPER_UA_IN_CC_MODE 200000
  200. #define CC_MODE_TAPER_MAIN_ICL_UA 500000
  201. #define MAX_IOUT_UA 6300000
  202. #define MAX_1S_VOUT_UV 11700000
  203. #define THERMAL_SUSPEND_DECIDEGC 1400
  204. #define DIV2_CP_MASTER 0
  205. #define DIV2_CP_SLAVE 1
  206. #define COMBO_PRE_REGULATOR 2
  207. #define SMB1394_DIV2_CP_PRY 3
  208. #define SMB1394_DIV2_CP_SECY 4
  209. #define IS_SMB1394(role) \
  210. (role == SMB1394_DIV2_CP_PRY || role == SMB1394_DIV2_CP_SECY)
  211. enum isns_mode {
  212. ISNS_MODE_OFF = 0,
  213. ISNS_MODE_ACTIVE,
  214. ISNS_MODE_STANDBY,
  215. };
  216. enum ovp {
  217. OVP_17P7V = 0,
  218. OVP_14V,
  219. OVP_22P2V,
  220. OVP_7P3,
  221. };
  222. enum {
  223. /* Perph0 IRQs */
  224. CFLY_HARD_FAULT_LATCH_IRQ,
  225. TEMP_SHDWN_IRQ,
  226. VOUT_UV_LATH_IRQ,
  227. DIV2_IREV_LATCH_IRQ,
  228. WLS_IN_UVLO_IRQ,
  229. USB_IN_UVLO_IRQ,
  230. WLS_IN_OVLO_IRQ,
  231. USB_IN_OVLO_IRQ,
  232. /* Perph1 IRQs */
  233. BK_IIN_REG_IRQ,
  234. CFLY_SS_DONE_IRQ,
  235. EN_DCDC_IRQ,
  236. ITERM_3LVL_LATCH_IRQ,
  237. VOUT_OV_3LB_IRQ,
  238. DIV2_ILIM_IRQ,
  239. DIV2_WIN_UV_IRQ,
  240. DIV2_WIN_OV_IRQ,
  241. /* Perph2 IRQs */
  242. IN_3LVL_MODE_IRQ,
  243. DIV2_MODE_IRQ,
  244. BK_CV_REG_IRQ,
  245. BK_CC_REG_IRQ,
  246. SS_DAC_INT_IRQ,
  247. SMB_EN_RISE_IRQ,
  248. SMB_EN_FALL_IRQ,
  249. /* End */
  250. NUM_IRQS,
  251. };
  252. struct smb_irq {
  253. const char *name;
  254. const irq_handler_t handler;
  255. const bool wake;
  256. int shift;
  257. };
  258. static const struct smb_irq smb_irqs[];
  259. struct smb1398_chip {
  260. struct device *dev;
  261. struct regmap *regmap;
  262. u8 rev4;
  263. struct wakeup_source *ws;
  264. struct iio_channel *die_temp_chan;
  265. unsigned int nchannels;
  266. struct iio_channel **cp_slave_iio_chan_list;
  267. struct iio_chan_spec *cp_iio_chan_ids;
  268. struct iio_channel **smb5_iio_chan_list;
  269. struct power_supply *div2_cp_master_psy;
  270. struct power_supply *div2_cp_slave_psy;
  271. struct power_supply *pre_regulator_psy;
  272. struct power_supply *batt_psy;
  273. struct power_supply *dc_psy;
  274. struct power_supply *usb_psy;
  275. struct notifier_block nb;
  276. struct votable *awake_votable;
  277. struct votable *div2_cp_disable_votable;
  278. struct votable *div2_cp_slave_disable_votable;
  279. struct votable *div2_cp_ilim_votable;
  280. struct votable *pre_regulator_iout_votable;
  281. struct votable *pre_regulator_vout_votable;
  282. struct votable *fcc_votable;
  283. struct votable *fv_votable;
  284. struct votable *fcc_main_votable;
  285. struct votable *usb_icl_votable;
  286. struct work_struct status_change_work;
  287. struct work_struct taper_work;
  288. struct mutex die_chan_lock;
  289. spinlock_t status_change_lock;
  290. int irqs[NUM_IRQS];
  291. int die_temp;
  292. int div2_cp_min_ilim_ua;
  293. int ilim_ua_disable_div2_cp_slave;
  294. int max_cutoff_soc;
  295. int taper_entry_fv;
  296. int div2_irq_status;
  297. u32 div2_cp_role;
  298. u32 pl_output_mode;
  299. u32 pl_input_mode;
  300. enum isns_mode current_capability;
  301. int cc_mode_taper_main_icl_ua;
  302. int cp_status1;
  303. int cp_status2;
  304. int cp_enable;
  305. int cp_isns_master;
  306. int cp_isns_slave;
  307. int cp_ilim;
  308. int adapter_type;
  309. bool status_change_running;
  310. bool taper_work_running;
  311. bool cutoff_soc_checked;
  312. bool smb_en;
  313. bool switcher_en;
  314. bool slave_en;
  315. bool in_suspend;
  316. bool disabled;
  317. bool usb_present;
  318. };
  319. struct cp_iio_prop_channels {
  320. const char *datasheet_name;
  321. int channel_no;
  322. enum iio_chan_type type;
  323. long info_mask;
  324. };
  325. #define SMB1398_CHAN(_dname, _chno, _type, _mask) \
  326. { \
  327. .datasheet_name = _dname, \
  328. .channel_no = _chno, \
  329. .type = _type, \
  330. .info_mask = _mask, \
  331. },
  332. #define SMB1398_CHAN_CUR(_dname, _chno) \
  333. SMB1398_CHAN(_dname, _chno, IIO_CURRENT, \
  334. BIT(IIO_CHAN_INFO_PROCESSED))
  335. #define SMB1398_CHAN_TEMP(_dname, _chno) \
  336. SMB1398_CHAN(_dname, _chno, IIO_TEMP, \
  337. BIT(IIO_CHAN_INFO_PROCESSED))
  338. #define SMB1398_CHAN_INDEX(_dname, _chno) \
  339. SMB1398_CHAN(_dname, _chno, IIO_INDEX, \
  340. BIT(IIO_CHAN_INFO_PROCESSED))
  341. static int smb1398_read(struct smb1398_chip *chip, u16 reg, u8 *val)
  342. {
  343. int rc = 0, value = 0;
  344. rc = regmap_read(chip->regmap, reg, &value);
  345. if (rc < 0)
  346. dev_err(chip->dev, "Couldn't read register 0x%x, rc=%d\n",
  347. reg, rc);
  348. else
  349. *val = (u8)value;
  350. return rc;
  351. }
  352. static int smb1398_masked_write(struct smb1398_chip *chip,
  353. u16 reg, u8 mask, u8 val)
  354. {
  355. int rc = 0;
  356. rc = regmap_update_bits(chip->regmap, reg, mask, val);
  357. if (rc < 0)
  358. dev_err(chip->dev, "Couldn't update register 0x%x to 0x%x with mask 0x%x, rc=%d\n",
  359. reg, val, mask, rc);
  360. return rc;
  361. }
  362. enum iio_type {
  363. CP_SLAVE,
  364. QPNP_SMB5,
  365. };
  366. enum cp_slave_channels {
  367. CURRENT_CAPABILITY = 0,
  368. CP_ENABLE,
  369. CP_INPUT_CURRENT_MAX,
  370. };
  371. enum smb5_iio_channels {
  372. REAL_TYPE = 0,
  373. ADAPTER_CC_MODE,
  374. PD_CURRENT_MAX,
  375. INPUT_CURRENT_SETTLED,
  376. SMB_EN_MODE,
  377. SMB_EN_REASON,
  378. };
  379. static const char * const cp_slave_iio_chans[] = {
  380. [CURRENT_CAPABILITY] = "cp_current_capability",
  381. [CP_ENABLE] = "cp_enable",
  382. [CP_INPUT_CURRENT_MAX] = "cp_input_current_max",
  383. };
  384. static const char * const cp_smb5_ext_iio_chan[] = {
  385. [REAL_TYPE] = "real_type",
  386. [ADAPTER_CC_MODE] = "adapter_cc_mode",
  387. [PD_CURRENT_MAX] = "pd_current_max",
  388. [INPUT_CURRENT_SETTLED] = "input_current_settled",
  389. [SMB_EN_MODE] = "smb_en_mode",
  390. [SMB_EN_REASON] = "smb_en_reason",
  391. };
  392. static int cp_read_iio_prop(struct smb1398_chip *chip,
  393. enum iio_type type, int iio_chan_id, int *val)
  394. {
  395. struct iio_channel *iio_chan;
  396. int rc;
  397. if (type == QPNP_SMB5) {
  398. if (IS_ERR_OR_NULL(chip->smb5_iio_chan_list))
  399. return -ENODEV;
  400. iio_chan = chip->smb5_iio_chan_list[iio_chan_id];
  401. } else {
  402. pr_err_ratelimited("iio_type %d is not supported\n", type);
  403. return -EINVAL;
  404. }
  405. rc = iio_read_channel_processed(iio_chan, val);
  406. return rc < 0 ? rc : 0;
  407. }
  408. static int cp_write_iio_prop(struct smb1398_chip *chip,
  409. enum iio_type type, int iio_chan_id, int val)
  410. {
  411. struct iio_channel *iio_chan;
  412. if (type == CP_SLAVE) {
  413. if (IS_ERR_OR_NULL(chip->cp_slave_iio_chan_list))
  414. return -ENODEV;
  415. iio_chan = chip->cp_slave_iio_chan_list[iio_chan_id];
  416. } else {
  417. pr_err_ratelimited("iio_type %d is not supported\n", type);
  418. return -EINVAL;
  419. }
  420. return iio_write_channel_raw(iio_chan, val);
  421. }
  422. static int smb1398_get_enable_status(struct smb1398_chip *chip)
  423. {
  424. int rc = 0;
  425. u8 val;
  426. bool switcher_en = false;
  427. rc = smb1398_read(chip, MODE_STATUS_REG, &val);
  428. if (rc < 0)
  429. return rc;
  430. chip->smb_en = !!(val & SMB_EN);
  431. chip->switcher_en = !!(val & PRE_EN_DCDC);
  432. chip->slave_en = !!(val & DIV2_EN_SLAVE);
  433. rc = smb1398_read(chip, MISC_SL_SWITCH_EN_REG, &val);
  434. if (rc < 0)
  435. return rc;
  436. switcher_en = !!(val & EN_SWITCHER);
  437. chip->switcher_en = switcher_en && chip->switcher_en;
  438. dev_dbg(chip->dev, "smb_en = %d, switcher_en = %d, slave_en = %d\n",
  439. chip->smb_en, chip->switcher_en, chip->slave_en);
  440. return rc;
  441. }
  442. static int smb1398_get_iin_ma(struct smb1398_chip *chip, int *iin_ma)
  443. {
  444. int rc = 0;
  445. u8 val;
  446. rc = smb1398_read(chip, IIN_SS_DAC_TARGET_REG, &val);
  447. if (rc < 0)
  448. return rc;
  449. *iin_ma = (val & IIN_SS_DAC_VALUE_MASK) * IIN_STEP_MA;
  450. dev_dbg(chip->dev, "get iin_ma = %dmA\n", *iin_ma);
  451. return rc;
  452. }
  453. static int smb1398_set_iin_ma(struct smb1398_chip *chip, int iin_ma)
  454. {
  455. int rc = 0;
  456. u8 val;
  457. val = iin_ma / IIN_STEP_MA;
  458. rc = smb1398_masked_write(chip, IIN_SS_DAC_TARGET_REG,
  459. IIN_SS_DAC_VALUE_MASK, val);
  460. if (rc < 0)
  461. return rc;
  462. dev_dbg(chip->dev, "set iin_ma = %dmA\n", iin_ma);
  463. return rc;
  464. }
  465. static int smb1398_set_ichg_ma(struct smb1398_chip *chip, int ichg_ma)
  466. {
  467. int rc = 0;
  468. u8 val;
  469. if (ichg_ma < 0 || ichg_ma > ICHG_SS_DAC_VALUE_MASK * ICHG_STEP_MA)
  470. return rc;
  471. val = ichg_ma / ICHG_STEP_MA;
  472. rc = smb1398_masked_write(chip, ICHG_SS_DAC_TARGET_REG,
  473. ICHG_SS_DAC_VALUE_MASK, val);
  474. dev_dbg(chip->dev, "set ichg %dmA\n", ichg_ma);
  475. return rc;
  476. }
  477. static int smb1398_get_ichg_ma(struct smb1398_chip *chip, int *ichg_ma)
  478. {
  479. int rc = 0;
  480. u8 val;
  481. rc = smb1398_read(chip, ICHG_SS_DAC_TARGET_REG, &val);
  482. if (rc < 0)
  483. return rc;
  484. *ichg_ma = (val & ICHG_SS_DAC_VALUE_MASK) * ICHG_STEP_MA;
  485. dev_dbg(chip->dev, "get ichg %dmA\n", *ichg_ma);
  486. return 0;
  487. }
  488. static int smb1398_set_1s_vout_mv(struct smb1398_chip *chip, int vout_mv)
  489. {
  490. int rc = 0;
  491. u8 val;
  492. if (vout_mv < VOUT_1S_MIN_MV)
  493. return -EINVAL;
  494. val = (vout_mv - VOUT_1S_MIN_MV) / VOUT_1S_STEP_MV;
  495. rc = smb1398_masked_write(chip, VOUT_DAC_TARGET_REG,
  496. VOUT_DAC_VALUE_MASK, val);
  497. if (rc < 0)
  498. return rc;
  499. return 0;
  500. }
  501. static int smb1398_get_1s_vout_mv(struct smb1398_chip *chip, int *vout_mv)
  502. {
  503. int rc;
  504. u8 val;
  505. rc = smb1398_read(chip, VOUT_DAC_TARGET_REG, &val);
  506. if (rc < 0)
  507. return rc;
  508. *vout_mv = (val & VOUT_DAC_VALUE_MASK) * VOUT_1S_STEP_MV +
  509. VOUT_1S_MIN_MV;
  510. return 0;
  511. }
  512. static int smb1398_get_die_temp(struct smb1398_chip *chip, int *temp)
  513. {
  514. int die_temp_deciC = 0, rc = 0;
  515. rc = smb1398_get_enable_status(chip);
  516. if (rc < 0)
  517. return rc;
  518. if (!chip->smb_en)
  519. return -ENODATA;
  520. mutex_lock(&chip->die_chan_lock);
  521. rc = iio_read_channel_processed(chip->die_temp_chan, &die_temp_deciC);
  522. mutex_unlock(&chip->die_chan_lock);
  523. if (rc < 0) {
  524. dev_err(chip->dev, "Couldn't read die_temp_chan, rc=%d\n", rc);
  525. } else {
  526. *temp = die_temp_deciC / 100;
  527. dev_dbg(chip->dev, "die temp %d\n", *temp);
  528. }
  529. return rc;
  530. }
  531. static int smb1398_div2_cp_get_status1(
  532. struct smb1398_chip *chip, u8 *status)
  533. {
  534. int rc = 0;
  535. u8 val;
  536. bool ilim, win_uv, win_ov;
  537. rc = smb1398_read(chip, PERPH1_INT_RT_STS_REG, &val);
  538. if (rc < 0)
  539. return rc;
  540. win_uv = !!(val & DIV2_WIN_UV_STS);
  541. win_ov = !!(val & DIV2_WIN_OV_STS);
  542. ilim = !!(val & DIV2_ILIM_STS);
  543. *status = ilim << 5 | win_uv << 1 | win_ov;
  544. dev_dbg(chip->dev, "status1 = 0x%x\n", *status);
  545. return rc;
  546. }
  547. static int smb1398_div2_cp_get_status2(
  548. struct smb1398_chip *chip, u8 *status)
  549. {
  550. int rc = 0;
  551. u8 val;
  552. bool smb_en, vin_ov, vin_uv, irev, tsd, switcher_off;
  553. rc = smb1398_read(chip, MODE_STATUS_REG, &val);
  554. if (rc < 0)
  555. return rc;
  556. smb_en = !!(val & SMB_EN);
  557. switcher_off = !(val & PRE_EN_DCDC);
  558. rc = smb1398_read(chip, PERPH1_INT_RT_STS_REG, &val);
  559. if (rc < 0)
  560. return rc;
  561. switcher_off = !(val & DIV2_CFLY_SS_DONE_STS) && switcher_off;
  562. rc = smb1398_read(chip, SWITCHER_OFF_VIN_STATUS_REG, &val);
  563. if (rc < 0)
  564. return rc;
  565. vin_ov = !!(val & USB_IN_OVLO);
  566. vin_uv = !!(val & USB_IN_UVLO);
  567. rc = smb1398_read(chip, SWITCHER_OFF_FAULT_REG, &val);
  568. if (rc < 0)
  569. return rc;
  570. irev = !!(val & DIV2_IREV_LATCH);
  571. tsd = !!(val & TEMP_SHDWN);
  572. *status = smb_en << 7 | vin_ov << 6 | vin_uv << 5
  573. | irev << 3 | tsd << 2 | switcher_off;
  574. dev_dbg(chip->dev, "status2 = 0x%x\n", *status);
  575. return rc;
  576. }
  577. static int smb1398_div2_cp_get_irq_status(
  578. struct smb1398_chip *chip, u8 *status)
  579. {
  580. int rc = 0;
  581. u8 val;
  582. bool ilim, irev, tsd, off_vin, off_win;
  583. rc = smb1398_read(chip, PERPH1_INT_RT_STS_REG, &val);
  584. if (rc < 0)
  585. return rc;
  586. ilim = !!(val & DIV2_ILIM_STS);
  587. off_win = !!(val & (DIV2_WIN_OV_STS | DIV2_WIN_UV_STS));
  588. rc = smb1398_read(chip, PERPH0_INT_RT_STS_REG, &val);
  589. if (rc < 0)
  590. return rc;
  591. irev = !!(val & DIV2_IREV_LATCH_STS);
  592. tsd = !!(val & TEMP_SHUTDOWN_STS);
  593. off_vin = !!(val & (USB_IN_OVLO_STS | USB_IN_UVLO_STS));
  594. *status = ilim << 6 | irev << 3 | tsd << 2 | off_vin << 1 | off_win;
  595. dev_dbg(chip->dev, "irq_status = 0x%x\n", *status);
  596. return rc;
  597. }
  598. static int smb1398_div2_cp_switcher_en(struct smb1398_chip *chip, bool en)
  599. {
  600. int rc;
  601. rc = smb1398_masked_write(chip, MISC_USB_WLS_SUSPEND_REG,
  602. USB_SUSPEND, en ? 0 : USB_SUSPEND);
  603. if (rc < 0) {
  604. dev_err(chip->dev, "Couldn't write USB_WLS_SUSPEND_REG, rc=%d\n",
  605. rc);
  606. return rc;
  607. }
  608. rc = smb1398_masked_write(chip, MISC_SL_SWITCH_EN_REG,
  609. EN_SWITCHER, en ? EN_SWITCHER : 0);
  610. if (rc < 0) {
  611. dev_err(chip->dev, "Couldn't write SWITCH_EN_REG, rc=%d\n", rc);
  612. return rc;
  613. }
  614. chip->switcher_en = en;
  615. dev_dbg(chip->dev, "%s switcher\n", en ? "enable" : "disable");
  616. return rc;
  617. }
  618. static int smb1398_div2_cp_isns_mode_control(
  619. struct smb1398_chip *chip, enum isns_mode mode)
  620. {
  621. int rc = 0;
  622. u8 mux_sel;
  623. switch (mode) {
  624. case ISNS_MODE_STANDBY:
  625. /* VTEMP */
  626. mux_sel = SEL_OUT_VTEMP;
  627. break;
  628. case ISNS_MODE_OFF:
  629. /* High-Z */
  630. mux_sel = SEL_OUT_HIGHZ;
  631. break;
  632. case ISNS_MODE_ACTIVE:
  633. /* IIN_FB */
  634. mux_sel = SEL_OUT_IIN_FB;
  635. break;
  636. default:
  637. return -EINVAL;
  638. }
  639. rc = smb1398_masked_write(chip, SSUPLY_TEMP_CTRL_REG,
  640. SEL_OUT_TEMP_MAX_MASK, mux_sel);
  641. if (rc < 0) {
  642. dev_err(chip->dev, "Couldn't set SSUPLY_TEMP_CTRL_REG, rc=%d\n",
  643. rc);
  644. return rc;
  645. }
  646. rc = smb1398_masked_write(chip, PERPH0_MISC_CFG2_REG,
  647. CFG_TEMP_PIN_ITEMP, 0);
  648. if (rc < 0) {
  649. dev_err(chip->dev, "Couldn't set PERPH0_MISC_CFG2_REG, rc=%d\n",
  650. rc);
  651. return rc;
  652. }
  653. return 0;
  654. }
  655. static inline int calculate_div2_cp_isns_ua(int temp)
  656. {
  657. /* ISNS = (2850 + (0.0034 * thermal_reading) / 0.32) * 1000 uA */
  658. return (2850 * 1000 + div_s64((s64)temp * 340, 32));
  659. }
  660. static struct iio_channel **get_ext_channels(struct device *dev,
  661. const char *const *channel_map, int size)
  662. {
  663. int i, rc = 0;
  664. struct iio_channel **iio_ch_ext;
  665. iio_ch_ext = devm_kcalloc(dev, size, sizeof(*iio_ch_ext), GFP_KERNEL);
  666. if (!iio_ch_ext)
  667. return ERR_PTR(-ENOMEM);
  668. for (i = 0; i < size; i++) {
  669. iio_ch_ext[i] = devm_iio_channel_get(dev, channel_map[i]);
  670. if (IS_ERR(iio_ch_ext[i])) {
  671. rc = PTR_ERR(iio_ch_ext[i]);
  672. if (rc != -EPROBE_DEFER)
  673. dev_err(dev, "%s channel unavailable, %d\n",
  674. channel_map[i], rc);
  675. return ERR_PTR(rc);
  676. }
  677. }
  678. return iio_ch_ext;
  679. }
  680. static bool is_cps_available(struct smb1398_chip *chip)
  681. {
  682. int rc = 0;
  683. struct iio_channel **iio_list;
  684. if (IS_ERR(chip->cp_slave_iio_chan_list))
  685. return false;
  686. if (!chip->cp_slave_iio_chan_list) {
  687. iio_list = get_ext_channels(chip->dev,
  688. cp_slave_iio_chans, ARRAY_SIZE(cp_slave_iio_chans));
  689. if (IS_ERR(iio_list)) {
  690. rc = PTR_ERR(iio_list);
  691. if (rc != -EPROBE_DEFER) {
  692. dev_err(chip->dev, "Failed to get channels, rc=%d\n",
  693. rc);
  694. chip->cp_slave_iio_chan_list = ERR_PTR(-EINVAL);
  695. }
  696. return false;
  697. }
  698. chip->cp_slave_iio_chan_list = iio_list;
  699. }
  700. return true;
  701. }
  702. static int smb1398_div2_cp_get_master_isns(
  703. struct smb1398_chip *chip, int *isns_ua)
  704. {
  705. int rc = 0, temp, val;
  706. rc = smb1398_get_enable_status(chip);
  707. if (rc < 0)
  708. return rc;
  709. if (!chip->smb_en)
  710. return -ENODATA;
  711. /*
  712. * Follow this procedure to read master CP ISNS:
  713. * set slave CP TEMP_MUX to HighZ;
  714. * set master CP TEMP_MUX to IIN_FB;
  715. * set DIV2_CP switch phase-shift to 0 deg;
  716. * read corresponding ADC channel in Kekaha;
  717. * set DIV2_CP switch phase-shif back to 90 deg;
  718. * set master CP TEMP_MUX to VTEMP;
  719. */
  720. mutex_lock(&chip->die_chan_lock);
  721. if (is_cps_available(chip)) {
  722. val = ISNS_MODE_OFF;
  723. rc = cp_write_iio_prop(chip, CP_SLAVE,
  724. CURRENT_CAPABILITY, val);
  725. if (rc < 0) {
  726. dev_err(chip->dev, "Couldn't set slave ISNS_MODE_OFF, rc=%d\n",
  727. rc);
  728. goto unlock;
  729. }
  730. }
  731. rc = smb1398_div2_cp_isns_mode_control(chip, ISNS_MODE_ACTIVE);
  732. if (rc < 0) {
  733. dev_err(chip->dev, "Couldn't set master ISNS_MODE_ACTIVE, rc=%d\n",
  734. rc);
  735. goto unlock;
  736. }
  737. rc = smb1398_masked_write(chip, PERPH0_DIV2_SLAVE,
  738. CFG_DIV2_SYNC_CLK_PHASE_90, 0);
  739. if (rc < 0) {
  740. dev_err(chip->dev, "Couldn't set PERPH0_DIV2_SLAVE, rc=%d\n",
  741. rc);
  742. goto unlock;
  743. }
  744. /* Delay for the phase switch to take effect */
  745. msleep(20);
  746. rc = iio_read_channel_processed(chip->die_temp_chan, &temp);
  747. if (rc < 0) {
  748. dev_err(chip->dev, "Couldn't read die_temp_chan, rc=%d\n", rc);
  749. goto unlock;
  750. }
  751. rc = smb1398_masked_write(chip, PERPH0_DIV2_SLAVE,
  752. CFG_DIV2_SYNC_CLK_PHASE_90, CFG_DIV2_SYNC_CLK_PHASE_90);
  753. if (rc < 0) {
  754. dev_err(chip->dev, "Couldn't set PERPH0_DIV2_SLAVE, rc=%d\n",
  755. rc);
  756. goto unlock;
  757. }
  758. rc = smb1398_div2_cp_isns_mode_control(chip, ISNS_MODE_STANDBY);
  759. if (rc < 0) {
  760. dev_err(chip->dev, "Couldn't set master ISNS_MODE_STANDBY, rc=%d\n",
  761. rc);
  762. goto unlock;
  763. }
  764. unlock:
  765. mutex_unlock(&chip->die_chan_lock);
  766. if (rc >= 0) {
  767. *isns_ua = calculate_div2_cp_isns_ua(temp);
  768. dev_dbg(chip->dev, "master isns = %duA\n", *isns_ua);
  769. }
  770. return rc;
  771. }
  772. static int smb1398_div2_cp_get_slave_isns(
  773. struct smb1398_chip *chip, int *isns_ua)
  774. {
  775. int temp = 0, rc, val;
  776. if (!is_cps_available(chip)) {
  777. *isns_ua = 0;
  778. return 0;
  779. }
  780. rc = smb1398_get_enable_status(chip);
  781. if (rc < 0)
  782. return rc;
  783. if (!chip->smb_en || !chip->slave_en)
  784. return -ENODATA;
  785. /*
  786. * Follow this procedure to read slave CP ISNS:
  787. * set master CP TEMP_MUX to HighZ;
  788. * set slave CP TEMP_MUX to IIN_FB;
  789. * set DIV2_CP switch phase-shift to 0 deg;
  790. * read corresponding ADC channel in Kekaha;
  791. * set DIV2_CP switch phase-shif back to 90 deg;
  792. * set master CP TEMP_MUX to VTEMP;
  793. */
  794. mutex_lock(&chip->die_chan_lock);
  795. rc = smb1398_div2_cp_isns_mode_control(chip, ISNS_MODE_OFF);
  796. if (rc < 0) {
  797. dev_err(chip->dev, "Couldn't set master ISNS_MODE_OFF, rc=%d\n",
  798. rc);
  799. goto unlock;
  800. }
  801. val = ISNS_MODE_ACTIVE;
  802. rc = cp_write_iio_prop(chip, CP_SLAVE, CURRENT_CAPABILITY, val);
  803. if (rc < 0) {
  804. dev_err(chip->dev, "Couldn't set slave ISNS_MODE_ACTIVE, rc=%d\n",
  805. rc);
  806. goto unlock;
  807. }
  808. rc = smb1398_masked_write(chip, PERPH0_DIV2_SLAVE,
  809. CFG_DIV2_SYNC_CLK_PHASE_90, 0);
  810. if (rc < 0) {
  811. dev_err(chip->dev, "Couldn't set PERPH0_DIV2_SLAVE, rc=%d\n",
  812. rc);
  813. goto unlock;
  814. }
  815. /* Delay for the phase switch to take effect */
  816. msleep(20);
  817. rc = iio_read_channel_processed(chip->die_temp_chan, &temp);
  818. if (rc < 0) {
  819. dev_err(chip->dev, "Couldn't get die_temp_chan, rc=%d\n", rc);
  820. goto unlock;
  821. }
  822. rc = smb1398_masked_write(chip, PERPH0_DIV2_SLAVE,
  823. CFG_DIV2_SYNC_CLK_PHASE_90, CFG_DIV2_SYNC_CLK_PHASE_90);
  824. if (rc < 0) {
  825. dev_err(chip->dev, "Couldn't set PERPH0_DIV2_SLAVE, rc=%d\n",
  826. rc);
  827. goto unlock;
  828. }
  829. rc = smb1398_div2_cp_isns_mode_control(chip, ISNS_MODE_STANDBY);
  830. if (rc < 0) {
  831. dev_err(chip->dev, "Couldn't set master ISNS_MODE_STANDBY, rc=%d\n",
  832. rc);
  833. goto unlock;
  834. }
  835. unlock:
  836. mutex_unlock(&chip->die_chan_lock);
  837. if (rc >= 0) {
  838. *isns_ua = calculate_div2_cp_isns_ua(temp);
  839. dev_dbg(chip->dev, "slave isns = %duA\n", *isns_ua);
  840. }
  841. return rc;
  842. }
  843. static void smb1398_toggle_switcher(struct smb1398_chip *chip)
  844. {
  845. int rc = 0;
  846. /*
  847. * Disable DIV2_ILIM detection before toggling the switcher
  848. * to prevent any ILIM interrupt storm while the toggling
  849. */
  850. rc = smb1398_masked_write(chip, DIV2_CURRENT_REG, DIV2_EN_ILIM_DET, 0);
  851. if (rc < 0)
  852. dev_err(chip->dev, "Couldn't disable EN_ILIM_DET, rc=%d\n", rc);
  853. vote(chip->div2_cp_disable_votable, SWITCHER_TOGGLE_VOTER, true, 0);
  854. /* Delay for toggling switcher */
  855. usleep_range(20, 30);
  856. vote(chip->div2_cp_disable_votable, SWITCHER_TOGGLE_VOTER, false, 0);
  857. rc = smb1398_masked_write(chip, DIV2_CURRENT_REG,
  858. DIV2_EN_ILIM_DET, DIV2_EN_ILIM_DET);
  859. if (rc < 0)
  860. dev_err(chip->dev, "Couldn't disable EN_ILIM_DET, rc=%d\n", rc);
  861. }
  862. #define DEFAULT_HVDCP3_MIN_ICL_UA 1000000
  863. static int smb1398_div2_cp_get_min_icl(struct smb1398_chip *chip)
  864. {
  865. int min_ilim = chip->div2_cp_min_ilim_ua;
  866. /* Use max(dt_min_icl, 1A) for HVDCP3 */
  867. if (chip->adapter_type == QTI_POWER_SUPPLY_TYPE_USB_HVDCP_3)
  868. min_ilim = max(chip->div2_cp_min_ilim_ua,
  869. DEFAULT_HVDCP3_MIN_ICL_UA);
  870. return min_ilim;
  871. }
  872. static char *div2_cp_get_model_name(struct smb1398_chip *chip)
  873. {
  874. if (IS_SMB1394(chip->div2_cp_role))
  875. return "SMB1394";
  876. if (chip->rev4 > 2)
  877. return "SMB1398_V3";
  878. else if (chip->rev4 == 2)
  879. return "SMB1398_V2";
  880. else
  881. return "SMB1398_V1";
  882. }
  883. static int smb1398_toggle_uvlo(struct smb1398_chip *chip)
  884. {
  885. int rc;
  886. rc = smb1398_masked_write(chip, PERPH0_SOVP_CFG0_REG,
  887. CFG_OVP_IGNORE_UVLO, CFG_OVP_IGNORE_UVLO);
  888. if (rc < 0)
  889. dev_err(chip->dev, "Couldn't write IGNORE_UVLO rc=%d\n", rc);
  890. rc = smb1398_masked_write(chip, PERPH0_SOVP_CFG0_REG,
  891. CFG_OVP_IGNORE_UVLO, 0);
  892. if (rc < 0)
  893. dev_err(chip->dev, "Couldn't write IGNORE_UVLO, rc=%d\n", rc);
  894. return rc;
  895. }
  896. static enum power_supply_property div2_cp_master_props[] = {
  897. POWER_SUPPLY_PROP_MODEL_NAME,
  898. };
  899. static int div2_cp_master_get_prop(struct power_supply *psy,
  900. enum power_supply_property prop,
  901. union power_supply_propval *val)
  902. {
  903. struct smb1398_chip *chip = power_supply_get_drvdata(psy);
  904. int rc = 0;
  905. switch (prop) {
  906. case POWER_SUPPLY_PROP_MODEL_NAME:
  907. val->strval = div2_cp_get_model_name(chip);
  908. break;
  909. default:
  910. rc = -EINVAL;
  911. break;
  912. }
  913. return rc;
  914. }
  915. static struct power_supply_desc div2_cp_master_desc = {
  916. .name = "charge_pump_master",
  917. .type = POWER_SUPPLY_TYPE_MAINS,
  918. .properties = div2_cp_master_props,
  919. .num_properties = ARRAY_SIZE(div2_cp_master_props),
  920. .get_property = div2_cp_master_get_prop,
  921. };
  922. static int smb1398_init_div2_cp_master_psy(struct smb1398_chip *chip)
  923. {
  924. struct power_supply_config div2_cp_master_psy_cfg = {};
  925. int rc = 0;
  926. div2_cp_master_psy_cfg.drv_data = chip;
  927. div2_cp_master_psy_cfg.of_node = chip->dev->of_node;
  928. chip->div2_cp_master_psy = devm_power_supply_register(chip->dev,
  929. &div2_cp_master_desc, &div2_cp_master_psy_cfg);
  930. if (IS_ERR(chip->div2_cp_master_psy)) {
  931. rc = PTR_ERR(chip->div2_cp_master_psy);
  932. dev_err(chip->dev, "Register div2_cp_master power supply failed, rc=%d\n",
  933. rc);
  934. return rc;
  935. }
  936. return 0;
  937. }
  938. static bool is_psy_voter_available(struct smb1398_chip *chip)
  939. {
  940. if (!chip->batt_psy) {
  941. chip->batt_psy = power_supply_get_by_name("battery");
  942. if (!chip->batt_psy) {
  943. dev_dbg(chip->dev, "Couldn't find battery psy\n");
  944. return false;
  945. }
  946. }
  947. if (!chip->usb_psy) {
  948. chip->usb_psy = power_supply_get_by_name("usb");
  949. if (!chip->usb_psy) {
  950. dev_dbg(chip->dev, "Couldn't find usb psy\n");
  951. return false;
  952. }
  953. }
  954. if (!chip->dc_psy) {
  955. chip->dc_psy = power_supply_get_by_name("dc");
  956. if (!chip->dc_psy) {
  957. dev_dbg(chip->dev, "Couldn't find DC psy\n");
  958. return false;
  959. }
  960. }
  961. if (!chip->fcc_votable) {
  962. chip->fcc_votable = find_votable("FCC");
  963. if (!chip->fcc_votable) {
  964. dev_dbg(chip->dev, "Couldn't find FCC voltable\n");
  965. return false;
  966. }
  967. }
  968. if (!chip->fv_votable) {
  969. chip->fv_votable = find_votable("FV");
  970. if (!chip->fv_votable) {
  971. dev_dbg(chip->dev, "Couldn't find FV voltable\n");
  972. return false;
  973. }
  974. }
  975. if (!chip->usb_icl_votable) {
  976. chip->usb_icl_votable = find_votable("USB_ICL");
  977. if (!chip->usb_icl_votable) {
  978. dev_dbg(chip->dev, "Couldn't find USB_ICL voltable\n");
  979. return false;
  980. }
  981. }
  982. if (!chip->fcc_main_votable) {
  983. chip->fcc_main_votable = find_votable("FCC_MAIN");
  984. if (!chip->fcc_main_votable) {
  985. dev_dbg(chip->dev, "Couldn't find FCC_MAIN voltable\n");
  986. return false;
  987. }
  988. }
  989. return true;
  990. }
  991. static bool is_cutoff_soc_reached(struct smb1398_chip *chip)
  992. {
  993. int rc;
  994. union power_supply_propval pval = {0};
  995. if (!chip->batt_psy)
  996. goto err;
  997. rc = power_supply_get_property(chip->batt_psy,
  998. POWER_SUPPLY_PROP_CAPACITY, &pval);
  999. if (rc < 0) {
  1000. dev_err(chip->dev, "Couldn't get battery soc, rc=%d\n", rc);
  1001. goto err;
  1002. }
  1003. if (pval.intval >= chip->max_cutoff_soc)
  1004. return true;
  1005. err:
  1006. return false;
  1007. }
  1008. static bool is_adapter_in_cc_mode(struct smb1398_chip *chip)
  1009. {
  1010. int rc, val = 0;
  1011. if (IS_ERR_OR_NULL(chip->smb5_iio_chan_list))
  1012. return false;
  1013. rc = cp_read_iio_prop(chip, QPNP_SMB5, ADAPTER_CC_MODE, &val);
  1014. if (rc < 0) {
  1015. dev_err(chip->dev, "Couldn't get ADAPTER_CC_MODE, rc=%d\n", rc);
  1016. return false;
  1017. }
  1018. return !!val;
  1019. }
  1020. static int smb1398_awake_vote_cb(struct votable *votable,
  1021. void *data, int awake, const char *client)
  1022. {
  1023. struct smb1398_chip *chip = (struct smb1398_chip *)data;
  1024. if (awake)
  1025. pm_stay_awake(chip->dev);
  1026. else
  1027. pm_relax(chip->dev);
  1028. return 0;
  1029. }
  1030. static int smb1398_div2_cp_disable_vote_cb(struct votable *votable,
  1031. void *data, int disable, const char *client)
  1032. {
  1033. struct smb1398_chip *chip = (struct smb1398_chip *)data;
  1034. int rc = 0;
  1035. if (!is_psy_voter_available(chip) || chip->in_suspend)
  1036. return -EAGAIN;
  1037. rc = smb1398_div2_cp_switcher_en(chip, !disable);
  1038. if (rc < 0) {
  1039. dev_err(chip->dev, "%s switcher failed, rc=%d\n",
  1040. !!disable ? "disable" : "enable", rc);
  1041. return rc;
  1042. }
  1043. if (is_cps_available(chip))
  1044. vote(chip->div2_cp_slave_disable_votable, MAIN_DISABLE_VOTER,
  1045. !!disable ? true : false, 0);
  1046. if (chip->div2_cp_master_psy && (disable != chip->disabled))
  1047. power_supply_changed(chip->div2_cp_master_psy);
  1048. chip->disabled = disable;
  1049. return 0;
  1050. }
  1051. static int smb1398_div2_cp_slave_disable_vote_cb(struct votable *votable,
  1052. void *data, int disable, const char *client)
  1053. {
  1054. struct smb1398_chip *chip = (struct smb1398_chip *)data;
  1055. u16 reg;
  1056. u8 val;
  1057. int rc, ilim_ua, value;
  1058. if (!is_cps_available(chip))
  1059. return -ENODEV;
  1060. reg = MISC_SL_SWITCH_EN_REG;
  1061. val = !!disable ? 0 : EN_SLAVE;
  1062. rc = smb1398_masked_write(chip, reg, EN_SLAVE, val);
  1063. if (rc < 0) {
  1064. dev_err(chip->dev, "Couldn't write slave_en, rc=%d\n", rc);
  1065. return rc;
  1066. }
  1067. value = !disable;
  1068. rc = cp_write_iio_prop(chip, CP_SLAVE, CP_ENABLE, value);
  1069. if (rc < 0) {
  1070. dev_err(chip->dev, "%s slave switcher failed, rc=%d\n",
  1071. !!disable ? "disable" : "enable", rc);
  1072. return rc;
  1073. }
  1074. /* Re-distribute ILIM to Master CP when Slave is disabled */
  1075. if (disable && (chip->div2_cp_ilim_votable)) {
  1076. ilim_ua = get_effective_result_locked(
  1077. chip->div2_cp_ilim_votable);
  1078. ilim_ua = (ilim_ua * DIV2_ILIM_CFG_PCT) / 100;
  1079. if (ilim_ua > DIV2_MAX_ILIM_UA)
  1080. ilim_ua = DIV2_MAX_ILIM_UA;
  1081. rc = smb1398_set_iin_ma(chip, ilim_ua / 1000);
  1082. if (rc < 0) {
  1083. dev_err(chip->dev, "Could't set CP master ilim, rc=%d\n",
  1084. rc);
  1085. return rc;
  1086. }
  1087. dev_dbg(chip->dev, "slave disabled, restore master CP ilim to %duA\n",
  1088. ilim_ua);
  1089. }
  1090. return rc;
  1091. }
  1092. static int smb1398_div2_cp_ilim_vote_cb(struct votable *votable,
  1093. void *data, int ilim_ua, const char *client)
  1094. {
  1095. struct smb1398_chip *chip = (struct smb1398_chip *)data;
  1096. int rc = 0, max_ilim_ua, min_ilim_ua, val;
  1097. bool slave_dis, split_ilim = false;
  1098. if (!is_psy_voter_available(chip) || chip->in_suspend)
  1099. return -EAGAIN;
  1100. if (!client)
  1101. return -EINVAL;
  1102. min_ilim_ua = smb1398_div2_cp_get_min_icl(chip);
  1103. ilim_ua = (ilim_ua * DIV2_ILIM_CFG_PCT) / 100;
  1104. max_ilim_ua = is_cps_available(chip) ?
  1105. DIV2_MAX_ILIM_DUAL_CP_UA : DIV2_MAX_ILIM_UA;
  1106. ilim_ua = min(ilim_ua, max_ilim_ua);
  1107. if (ilim_ua < min_ilim_ua) {
  1108. dev_dbg(chip->dev, "ilim %duA is too low to config CP charging\n",
  1109. ilim_ua);
  1110. vote(chip->div2_cp_disable_votable, ILIM_VOTER, true, 0);
  1111. } else {
  1112. if (is_cps_available(chip)) {
  1113. split_ilim = true;
  1114. slave_dis = ilim_ua < (2 * min_ilim_ua);
  1115. vote(chip->div2_cp_slave_disable_votable, ILIM_VOTER,
  1116. slave_dis, 0);
  1117. slave_dis = !!get_effective_result(
  1118. chip->div2_cp_slave_disable_votable);
  1119. if (slave_dis)
  1120. split_ilim = false;
  1121. }
  1122. if (split_ilim) {
  1123. ilim_ua /= 2;
  1124. val = ilim_ua;
  1125. rc = cp_write_iio_prop(chip, CP_SLAVE,
  1126. CP_INPUT_CURRENT_MAX, val);
  1127. if (rc < 0)
  1128. dev_err(chip->dev, "Couldn't set CP slave ilim, rc=%d\n",
  1129. rc);
  1130. dev_dbg(chip->dev, "set CP slave ilim to %duA\n",
  1131. ilim_ua);
  1132. }
  1133. rc = smb1398_set_iin_ma(chip, ilim_ua / 1000);
  1134. if (rc < 0) {
  1135. dev_err(chip->dev, "Couldn't set CP master ilim, rc=%d\n",
  1136. rc);
  1137. return rc;
  1138. }
  1139. dev_dbg(chip->dev, "set CP master ilim to %duA\n", ilim_ua);
  1140. vote(chip->div2_cp_disable_votable, ILIM_VOTER, false, 0);
  1141. }
  1142. return 0;
  1143. }
  1144. static void smb1398_destroy_votables(struct smb1398_chip *chip)
  1145. {
  1146. destroy_votable(chip->awake_votable);
  1147. destroy_votable(chip->div2_cp_disable_votable);
  1148. destroy_votable(chip->div2_cp_ilim_votable);
  1149. destroy_votable(chip->div2_cp_slave_disable_votable);
  1150. }
  1151. static int smb1398_div2_cp_create_votables(struct smb1398_chip *chip)
  1152. {
  1153. int rc;
  1154. chip->awake_votable = create_votable("SMB1398_AWAKE",
  1155. VOTE_SET_ANY, smb1398_awake_vote_cb, chip);
  1156. if (IS_ERR_OR_NULL(chip->awake_votable))
  1157. return PTR_ERR_OR_ZERO(chip->awake_votable);
  1158. chip->div2_cp_disable_votable = create_votable("CP_DISABLE",
  1159. VOTE_SET_ANY, smb1398_div2_cp_disable_vote_cb, chip);
  1160. if (IS_ERR_OR_NULL(chip->div2_cp_disable_votable)) {
  1161. rc = PTR_ERR_OR_ZERO(chip->div2_cp_disable_votable);
  1162. goto destroy;
  1163. }
  1164. chip->div2_cp_slave_disable_votable = create_votable("CP_SLAVE_DISABLE",
  1165. VOTE_SET_ANY, smb1398_div2_cp_slave_disable_vote_cb,
  1166. chip);
  1167. if (IS_ERR_OR_NULL(chip->div2_cp_slave_disable_votable)) {
  1168. rc = PTR_ERR_OR_ZERO(chip->div2_cp_slave_disable_votable);
  1169. goto destroy;
  1170. }
  1171. chip->div2_cp_ilim_votable = create_votable("CP_ILIM",
  1172. VOTE_MIN, smb1398_div2_cp_ilim_vote_cb, chip);
  1173. if (IS_ERR_OR_NULL(chip->div2_cp_ilim_votable)) {
  1174. rc = PTR_ERR_OR_ZERO(chip->div2_cp_ilim_votable);
  1175. goto destroy;
  1176. }
  1177. vote(chip->div2_cp_disable_votable, USER_VOTER, true, 0);
  1178. vote(chip->div2_cp_disable_votable, CUTOFF_SOC_VOTER,
  1179. is_cutoff_soc_reached(chip), 0);
  1180. /*
  1181. * In case SMB1398 probe happens after FCC value has been configured,
  1182. * update ilim vote to reflect FCC / 2 value, this is only applicable
  1183. * when SMB1398 is directly connected to VBAT.
  1184. */
  1185. if (is_psy_voter_available(chip) &&
  1186. (chip->pl_output_mode != QTI_POWER_SUPPLY_PL_OUTPUT_VPH))
  1187. vote(chip->div2_cp_ilim_votable, FCC_VOTER, true,
  1188. get_effective_result(chip->fcc_votable) / 2);
  1189. return 0;
  1190. destroy:
  1191. smb1398_destroy_votables(chip);
  1192. return 0;
  1193. }
  1194. static irqreturn_t smb1398_default_irq_handler(int irq, void *data)
  1195. {
  1196. struct smb1398_chip *chip = data;
  1197. int rc, i;
  1198. bool switcher_en = chip->switcher_en;
  1199. for (i = 0; i < NUM_IRQS; i++) {
  1200. if (irq == chip->irqs[i]) {
  1201. dev_dbg(chip->dev, "IRQ %s triggered\n",
  1202. smb_irqs[i].name);
  1203. chip->div2_irq_status |= 1 << smb_irqs[i].shift;
  1204. }
  1205. }
  1206. rc = smb1398_get_enable_status(chip);
  1207. if (rc < 0)
  1208. goto out;
  1209. if (chip->switcher_en != switcher_en)
  1210. if (chip->fcc_votable)
  1211. rerun_election(chip->fcc_votable);
  1212. out:
  1213. if (chip->div2_cp_master_psy)
  1214. power_supply_changed(chip->div2_cp_master_psy);
  1215. return IRQ_HANDLED;
  1216. }
  1217. static const struct smb_irq smb_irqs[] = {
  1218. /* useful IRQs from perph0 */
  1219. [TEMP_SHDWN_IRQ] = {
  1220. .name = "temp-shdwn",
  1221. .handler = smb1398_default_irq_handler,
  1222. .wake = true,
  1223. .shift = 2,
  1224. },
  1225. [DIV2_IREV_LATCH_IRQ] = {
  1226. .name = "div2-irev",
  1227. .handler = smb1398_default_irq_handler,
  1228. .wake = true,
  1229. .shift = 3,
  1230. },
  1231. [USB_IN_UVLO_IRQ] = {
  1232. .name = "usbin-uv",
  1233. .handler = smb1398_default_irq_handler,
  1234. .wake = true,
  1235. .shift = 1,
  1236. },
  1237. [USB_IN_OVLO_IRQ] = {
  1238. .name = "usbin-ov",
  1239. .handler = smb1398_default_irq_handler,
  1240. .wake = true,
  1241. .shift = 1,
  1242. },
  1243. /* useful IRQs from perph1 */
  1244. [DIV2_ILIM_IRQ] = {
  1245. .name = "div2-ilim",
  1246. .handler = smb1398_default_irq_handler,
  1247. .wake = true,
  1248. .shift = 6,
  1249. },
  1250. [DIV2_WIN_UV_IRQ] = {
  1251. .name = "div2-win-uv",
  1252. .handler = smb1398_default_irq_handler,
  1253. .wake = true,
  1254. .shift = 0,
  1255. },
  1256. [DIV2_WIN_OV_IRQ] = {
  1257. .name = "div2-win-ov",
  1258. .handler = smb1398_default_irq_handler,
  1259. .wake = true,
  1260. .shift = 0,
  1261. },
  1262. };
  1263. static int smb1398_get_irq_index_byname(const char *irq_name)
  1264. {
  1265. int i;
  1266. for (i = 0; i < ARRAY_SIZE(smb_irqs); i++) {
  1267. if (smb_irqs[i].name != NULL)
  1268. if (strcmp(smb_irqs[i].name, irq_name) == 0)
  1269. return i;
  1270. }
  1271. return -ENOENT;
  1272. }
  1273. static int smb1398_request_interrupt(struct smb1398_chip *chip,
  1274. struct device_node *node, const char *irq_name)
  1275. {
  1276. int rc = 0, irq, irq_index;
  1277. irq = of_irq_get_byname(node, irq_name);
  1278. if (irq < 0) {
  1279. dev_err(chip->dev, "Couldn't get irq %s failed\n", irq_name);
  1280. return irq;
  1281. }
  1282. irq_index = smb1398_get_irq_index_byname(irq_name);
  1283. if (irq_index < 0) {
  1284. dev_err(chip->dev, "%s IRQ is not defined\n", irq_name);
  1285. return irq_index;
  1286. }
  1287. if (!smb_irqs[irq_index].handler)
  1288. return 0;
  1289. /*
  1290. * Do not register temp-shdwn interrupt as it may misfire on toggling
  1291. * the SMB_EN input.
  1292. */
  1293. if (irq_index == TEMP_SHDWN_IRQ)
  1294. return 0;
  1295. rc = devm_request_threaded_irq(chip->dev, irq, NULL,
  1296. smb_irqs[irq_index].handler,
  1297. IRQF_ONESHOT, irq_name, chip);
  1298. if (rc < 0) {
  1299. dev_err(chip->dev, "Request interrupt for %s failed, rc=%d\n",
  1300. irq_name, rc);
  1301. return rc;
  1302. }
  1303. chip->irqs[irq_index] = irq;
  1304. if (smb_irqs[irq_index].wake)
  1305. enable_irq_wake(irq);
  1306. return 0;
  1307. }
  1308. static int smb1398_request_interrupts(struct smb1398_chip *chip)
  1309. {
  1310. struct device_node *node = chip->dev->of_node;
  1311. int rc = 0;
  1312. const char *name;
  1313. struct property *prop;
  1314. of_property_for_each_string(node, "interrupt-names", prop, name) {
  1315. rc = smb1398_request_interrupt(chip, node, name);
  1316. if (rc < 0)
  1317. return rc;
  1318. }
  1319. return 0;
  1320. }
  1321. #define ILIM_NR 10
  1322. #define ILIM_DR 8
  1323. #define ILIM_FACTOR(ilim) ((ilim * ILIM_NR) / ILIM_DR)
  1324. static void smb1398_configure_ilim(struct smb1398_chip *chip, int mode)
  1325. {
  1326. int rc, val = 0;
  1327. /* PPS adapter reply on the current advertised by the adapter */
  1328. if ((chip->pl_output_mode == QTI_POWER_SUPPLY_PL_OUTPUT_VPH)
  1329. && (mode == QTI_POWER_SUPPLY_CP_PPS)) {
  1330. rc = cp_read_iio_prop(chip, QPNP_SMB5, PD_CURRENT_MAX, &val);
  1331. if (rc < 0)
  1332. pr_err("Couldn't get PD CURRENT MAX rc=%d\n", rc);
  1333. else
  1334. vote(chip->div2_cp_ilim_votable, ICL_VOTER,
  1335. true, ILIM_FACTOR(val));
  1336. }
  1337. /* QC3.0/Wireless adapter rely on the settled AICL for USBMID_USBMID */
  1338. if ((chip->pl_input_mode == QTI_POWER_SUPPLY_PL_USBMID_USBMID)
  1339. && (mode == QTI_POWER_SUPPLY_CP_HVDCP3)) {
  1340. rc = cp_read_iio_prop(chip, QPNP_SMB5, INPUT_CURRENT_SETTLED,
  1341. &val);
  1342. if (rc < 0)
  1343. pr_err("Couldn't get usb aicl rc=%d\n", rc);
  1344. else
  1345. vote(chip->div2_cp_ilim_votable, ICL_VOTER,
  1346. true, val);
  1347. }
  1348. }
  1349. static void smb1398_status_change_work(struct work_struct *work)
  1350. {
  1351. struct smb1398_chip *chip = container_of(work,
  1352. struct smb1398_chip, status_change_work);
  1353. union power_supply_propval pval = {0};
  1354. int rc, val = 0;
  1355. if (!is_psy_voter_available(chip))
  1356. goto out;
  1357. /*
  1358. * If batt soc is not valid upon bootup, but becomes
  1359. * valid due to the battery discharging later, remove
  1360. * vote from CUTOFF_SOC_VOTER.
  1361. */
  1362. if (!is_cutoff_soc_reached(chip))
  1363. vote(chip->div2_cp_disable_votable, CUTOFF_SOC_VOTER, false, 0);
  1364. rc = power_supply_get_property(chip->usb_psy,
  1365. POWER_SUPPLY_PROP_PRESENT, &pval);
  1366. if (rc < 0) {
  1367. dev_err(chip->dev,
  1368. "Couldn't get USB PRESENT status, rc=%d\n", rc);
  1369. goto out;
  1370. }
  1371. if (chip->usb_present != !!pval.intval) {
  1372. chip->usb_present = !!pval.intval;
  1373. if (!chip->usb_present) /* USB has been removed */
  1374. smb1398_toggle_uvlo(chip);
  1375. }
  1376. rc = cp_read_iio_prop(chip, QPNP_SMB5, SMB_EN_MODE, &val);
  1377. if (rc < 0) {
  1378. dev_err(chip->dev, "Couldn't get SMB_EN_MODE, rc=%d\n", rc);
  1379. goto out;
  1380. }
  1381. /* If no CP charging started */
  1382. if (val != QTI_POWER_SUPPLY_CHARGER_SEC_CP) {
  1383. chip->cutoff_soc_checked = false;
  1384. vote(chip->div2_cp_slave_disable_votable, SRC_VOTER, true, 0);
  1385. vote(chip->div2_cp_slave_disable_votable,
  1386. TAPER_VOTER, false, 0);
  1387. vote(chip->div2_cp_disable_votable, TAPER_VOTER, false, 0);
  1388. vote(chip->div2_cp_disable_votable, SRC_VOTER, true, 0);
  1389. vote(chip->div2_cp_disable_votable, CUTOFF_SOC_VOTER, true, 0);
  1390. vote(chip->fcc_votable, CP_VOTER, false, 0);
  1391. vote(chip->div2_cp_ilim_votable, CC_MODE_VOTER, false, 0);
  1392. vote_override(chip->usb_icl_votable,
  1393. TAPER_MAIN_ICL_LIMIT_VOTER, false, 0);
  1394. goto out;
  1395. }
  1396. rc = cp_read_iio_prop(chip, QPNP_SMB5, REAL_TYPE, &chip->adapter_type);
  1397. if (rc < 0) {
  1398. dev_err(chip->dev, "Couldn't get REAL_TYPE, rc=%d\n", rc);
  1399. goto out;
  1400. }
  1401. rc = cp_read_iio_prop(chip, QPNP_SMB5, SMB_EN_REASON, &val);
  1402. if (rc < 0) {
  1403. dev_err(chip->dev, "Couldn't get SMB_EN_REASON failed, rc=%d\n",
  1404. rc);
  1405. goto out;
  1406. }
  1407. /*
  1408. * Slave SMB1398 is not required for the power-rating of QC3
  1409. */
  1410. if (val != QTI_POWER_SUPPLY_CP_HVDCP3)
  1411. vote(chip->div2_cp_slave_disable_votable, SRC_VOTER, false, 0);
  1412. if (val == QTI_POWER_SUPPLY_CP_NONE) {
  1413. vote(chip->div2_cp_disable_votable, SRC_VOTER, true, 0);
  1414. goto out;
  1415. }
  1416. vote(chip->div2_cp_disable_votable, SRC_VOTER, false, 0);
  1417. if (!chip->cutoff_soc_checked) {
  1418. vote(chip->div2_cp_disable_votable, CUTOFF_SOC_VOTER,
  1419. is_cutoff_soc_reached(chip), 0);
  1420. chip->cutoff_soc_checked = true;
  1421. }
  1422. if (val == QTI_POWER_SUPPLY_CP_WIRELESS) {
  1423. /*
  1424. * Get the max output current from the wireless PSY
  1425. * and set the DIV2 CP ilim accordingly
  1426. */
  1427. vote(chip->div2_cp_ilim_votable, ICL_VOTER, false, 0);
  1428. rc = power_supply_get_property(chip->dc_psy,
  1429. POWER_SUPPLY_PROP_CURRENT_MAX, &pval);
  1430. if (rc < 0)
  1431. dev_err(chip->dev, "Couldn't get DC CURRENT_MAX, rc=%d\n",
  1432. rc);
  1433. else
  1434. vote(chip->div2_cp_ilim_votable, WIRELESS_VOTER,
  1435. true, pval.intval);
  1436. } else {
  1437. vote(chip->div2_cp_ilim_votable, WIRELESS_VOTER, false, 0);
  1438. smb1398_configure_ilim(chip, pval.intval);
  1439. }
  1440. /*
  1441. * Remove CP Taper condition disable vote if float voltage
  1442. * increased in comparison to voltage at which it entered taper.
  1443. */
  1444. if (chip->taper_entry_fv < get_effective_result(chip->fv_votable)) {
  1445. vote(chip->div2_cp_slave_disable_votable,
  1446. TAPER_VOTER, false, 0);
  1447. vote(chip->div2_cp_disable_votable, TAPER_VOTER, false, 0);
  1448. }
  1449. /*
  1450. * all votes that would result in disabling the charge pump have
  1451. * been cast; ensure the charge pump is still enabled before
  1452. * continuing.
  1453. */
  1454. if (get_effective_result(chip->div2_cp_disable_votable))
  1455. goto out;
  1456. rc = power_supply_get_property(chip->batt_psy,
  1457. POWER_SUPPLY_PROP_CHARGE_TYPE, &pval);
  1458. if (rc < 0) {
  1459. dev_err(chip->dev, "Couldn't get CHARGE_TYPE, rc=%d\n",
  1460. rc);
  1461. goto out;
  1462. }
  1463. if (pval.intval == POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE) {
  1464. if (!chip->taper_work_running) {
  1465. chip->taper_work_running = true;
  1466. vote(chip->awake_votable, TAPER_VOTER, true, 0);
  1467. queue_work(system_long_wq, &chip->taper_work);
  1468. }
  1469. }
  1470. out:
  1471. pm_relax(chip->dev);
  1472. chip->status_change_running = false;
  1473. }
  1474. static int smb1398_notifier_cb(struct notifier_block *nb,
  1475. unsigned long event, void *data)
  1476. {
  1477. struct smb1398_chip *chip = container_of(nb, struct smb1398_chip, nb);
  1478. struct power_supply *psy = (struct power_supply *)data;
  1479. unsigned long flags;
  1480. if (event != PSY_EVENT_PROP_CHANGED)
  1481. return NOTIFY_OK;
  1482. if (strcmp(psy->desc->name, "battery") == 0 ||
  1483. strcmp(psy->desc->name, "usb") == 0 ||
  1484. strcmp(psy->desc->name, "cp_slave") == 0) {
  1485. spin_lock_irqsave(&chip->status_change_lock, flags);
  1486. if (!chip->status_change_running) {
  1487. chip->status_change_running = true;
  1488. pm_stay_awake(chip->dev);
  1489. schedule_work(&chip->status_change_work);
  1490. }
  1491. spin_unlock_irqrestore(&chip->status_change_lock, flags);
  1492. }
  1493. return NOTIFY_OK;
  1494. }
  1495. static void smb1398_taper_work(struct work_struct *work)
  1496. {
  1497. struct smb1398_chip *chip = container_of(work,
  1498. struct smb1398_chip, taper_work);
  1499. union power_supply_propval pval = {0};
  1500. int rc, fcc_ua, fv_uv, stepper_ua, main_fcc_ua = 0, min_ilim_ua;
  1501. bool slave_en;
  1502. if (!is_psy_voter_available(chip))
  1503. goto out;
  1504. if (!chip->fcc_main_votable)
  1505. chip->fcc_main_votable = find_votable("FCC_MAIN");
  1506. if (chip->fcc_main_votable)
  1507. main_fcc_ua = get_effective_result(chip->fcc_main_votable);
  1508. min_ilim_ua = smb1398_div2_cp_get_min_icl(chip);
  1509. chip->taper_entry_fv = get_effective_result(chip->fv_votable);
  1510. while (true) {
  1511. rc = power_supply_get_property(chip->batt_psy,
  1512. POWER_SUPPLY_PROP_CHARGE_TYPE, &pval);
  1513. if (rc < 0) {
  1514. dev_err(chip->dev, "Couldn't get CHARGE_TYPE, rc=%d\n",
  1515. rc);
  1516. goto out;
  1517. }
  1518. fv_uv = get_effective_result(chip->fv_votable);
  1519. if (fv_uv > chip->taper_entry_fv) {
  1520. dev_dbg(chip->dev, "Float voltage increased (%d-->%d)uV, exit!\n",
  1521. chip->taper_entry_fv, fv_uv);
  1522. vote(chip->div2_cp_disable_votable, TAPER_VOTER,
  1523. false, 0);
  1524. goto out;
  1525. } else {
  1526. chip->taper_entry_fv = fv_uv;
  1527. }
  1528. if (pval.intval == POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE) {
  1529. stepper_ua = is_adapter_in_cc_mode(chip) ?
  1530. TAPER_STEPPER_UA_IN_CC_MODE :
  1531. TAPER_STEPPER_UA_DEFAULT;
  1532. fcc_ua = get_effective_result(chip->fcc_votable)
  1533. - stepper_ua;
  1534. dev_dbg(chip->dev, "Taper stepper reduce FCC to %d\n",
  1535. fcc_ua);
  1536. vote(chip->fcc_votable, CP_VOTER, true, fcc_ua);
  1537. fcc_ua -= main_fcc_ua;
  1538. /*
  1539. * If total FCC is less than the minimum ILIM to
  1540. * keep CP master and slave online, disable CP.
  1541. */
  1542. if (fcc_ua < (min_ilim_ua * 2)) {
  1543. vote(chip->div2_cp_disable_votable,
  1544. TAPER_VOTER, true, 0);
  1545. /*
  1546. * When master CP is disabled, reset all votes
  1547. * on ICL to enable Main charger to pump
  1548. * charging current.
  1549. */
  1550. if (chip->usb_icl_votable)
  1551. vote_override(chip->usb_icl_votable,
  1552. TAPER_MAIN_ICL_LIMIT_VOTER,
  1553. false, 0);
  1554. goto out;
  1555. }
  1556. /*
  1557. * If total FCC is less than the minimum ILIM to keep
  1558. * slave CP online, disable slave, and set master CP
  1559. * ILIM to maximum to avoid ILIM IRQ storm.
  1560. */
  1561. slave_en = !get_effective_result(
  1562. chip->div2_cp_slave_disable_votable);
  1563. if ((fcc_ua < chip->ilim_ua_disable_div2_cp_slave) &&
  1564. slave_en && is_cps_available(chip)) {
  1565. dev_dbg(chip->dev, "Disable slave CP in taper\n");
  1566. vote(chip->div2_cp_slave_disable_votable,
  1567. TAPER_VOTER, true, 0);
  1568. vote_override(chip->div2_cp_ilim_votable,
  1569. CC_MODE_VOTER,
  1570. is_adapter_in_cc_mode(chip),
  1571. DIV2_MAX_ILIM_DUAL_CP_UA);
  1572. if (chip->usb_icl_votable)
  1573. vote_override(chip->usb_icl_votable,
  1574. TAPER_MAIN_ICL_LIMIT_VOTER,
  1575. is_adapter_in_cc_mode(chip),
  1576. chip->cc_mode_taper_main_icl_ua);
  1577. }
  1578. } else {
  1579. dev_dbg(chip->dev, "Not in taper, exit!\n");
  1580. }
  1581. msleep(500);
  1582. }
  1583. out:
  1584. dev_dbg(chip->dev, "exit taper work\n");
  1585. vote(chip->fcc_votable, CP_VOTER, false, 0);
  1586. vote(chip->awake_votable, TAPER_VOTER, false, 0);
  1587. chip->taper_work_running = false;
  1588. }
  1589. static int _smb1398_update_ovp(struct smb1398_chip *chip)
  1590. {
  1591. int rc = 0;
  1592. /* Ignore for REV2 and below */
  1593. if (chip->rev4 <= 2)
  1594. return 0;
  1595. rc = smb1398_masked_write(chip, PERPH0_SSUPPLY_CFG0_REG,
  1596. EN_HV_OV_OPTION2_BIT | EN_MV_OV_OPTION2_BIT,
  1597. EN_HV_OV_OPTION2_BIT);
  1598. if (rc < 0) {
  1599. dev_err(chip->dev,
  1600. "Couldn't set PERPH0_SSUPPLY_CFG0_REG rc=%d\n", rc);
  1601. return rc;
  1602. }
  1603. rc = smb1398_masked_write(chip, PERPH1_LOCK_SPARE_REG,
  1604. CFG_LOCK_SPARE1_MASK,
  1605. OVP_14V << CFG_LOCK_SPARE1_SHIFT);
  1606. if (rc < 0) {
  1607. dev_err(chip->dev,
  1608. "Couldn't set PERPH1_LOCK_SPARE_REG rc=%d\n", rc);
  1609. return rc;
  1610. }
  1611. return rc;
  1612. }
  1613. static int _smb1394_update_ovp(struct smb1398_chip *chip)
  1614. {
  1615. int rc = 0;
  1616. rc = smb1398_masked_write(chip, PERPH0_SOVP_CFG0_REG,
  1617. CFG_OVP_VSNS_THRESHOLD, CFG_OVP_VSNS_THRESHOLD);
  1618. if (rc < 0) {
  1619. dev_err(chip->dev, "Couldn't set PERPH0_SOVP_CFG0_REG rc=%d\n",
  1620. rc);
  1621. return rc;
  1622. }
  1623. rc = smb1398_masked_write(chip, PERPH0_OVLO_REF_REG,
  1624. SMB1394_INPUT_OVLO_CONF_MASK,
  1625. SMB1394_INPUT_OVLO_13P04V);
  1626. if (rc < 0) {
  1627. dev_err(chip->dev, "Couldn't set PERPH0_OVLO_REF rc=%d\n", rc);
  1628. return rc;
  1629. }
  1630. rc = smb1398_masked_write(chip, PERPH0_CFG_SDCDC_REG,
  1631. EN_WIN_OV_RISE_DEB_BIT, 0);
  1632. if (rc < 0)
  1633. dev_err(chip->dev, "Couldn't set PERPH0_CFG_SDCDC_REG rc=%d\n",
  1634. rc);
  1635. return rc;
  1636. }
  1637. static int smb1398_update_ovp(struct smb1398_chip *chip)
  1638. {
  1639. if (IS_SMB1394(chip->div2_cp_role))
  1640. return _smb1394_update_ovp(chip);
  1641. return _smb1398_update_ovp(chip);
  1642. }
  1643. static int smb1398_div2_cp_hw_init(struct smb1398_chip *chip)
  1644. {
  1645. int rc = 0;
  1646. rc = smb1398_update_ovp(chip);
  1647. if (rc < 0) {
  1648. dev_err(chip->dev, "Couldn't update OVP threshold rc=%d\n", rc);
  1649. return rc;
  1650. }
  1651. /* Configure window (Vin/2 - Vout) OV level to 500mV */
  1652. rc = smb1398_masked_write(chip, DIV2_PROTECTION_REG,
  1653. DIV2_WIN_OV_SEL_MASK, WIN_OV_500_MV);
  1654. if (rc < 0) {
  1655. dev_err(chip->dev, "Couldn't set WIN_OV_500_MV rc=%d\n", rc);
  1656. return rc;
  1657. }
  1658. /* Configure window (Vin/2 - Vout) UV level to 10mV */
  1659. rc = smb1398_masked_write(chip, NOLOCK_SPARE_REG,
  1660. DIV2_WIN_UV_SEL_BIT, 0);
  1661. if (rc < 0) {
  1662. dev_err(chip->dev, "Couldn't set WIN_UV_10_MV rc=%d\n", rc);
  1663. return rc;
  1664. }
  1665. /* Configure master TEMP pin to output Vtemp signal by default */
  1666. rc = smb1398_masked_write(chip, SSUPLY_TEMP_CTRL_REG,
  1667. SEL_OUT_TEMP_MAX_MASK, SEL_OUT_VTEMP);
  1668. if (rc < 0) {
  1669. dev_err(chip->dev, "Couldn't set SSUPLY_TEMP_CTRL_REG, rc=%d\n",
  1670. rc);
  1671. return rc;
  1672. }
  1673. /* Configure to use Vtemp signal */
  1674. rc = smb1398_masked_write(chip, PERPH0_MISC_CFG2_REG,
  1675. CFG_TEMP_PIN_ITEMP, 0);
  1676. if (rc < 0) {
  1677. dev_err(chip->dev, "Couldn't set PERPH0_MISC_CFG2_REG, rc=%d\n",
  1678. rc);
  1679. return rc;
  1680. }
  1681. /* Configure IREV threshold to 200mA */
  1682. rc = smb1398_masked_write(chip, PERPH0_DIV2_REF_CFG,
  1683. CFG_IREV_REF_BIT, 0);
  1684. if (rc < 0) {
  1685. pr_err("Couldn't configure IREV threshold rc=%d\n", rc);
  1686. return rc;
  1687. }
  1688. /* Initial configuration needed before enabling DIV2_CP operations */
  1689. rc = smb1398_masked_write(chip, MISC_DIV2_3LVL_CTRL_REG,
  1690. MISC_DIV2_3LVL_CTRL_MASK, 0x04);
  1691. if (rc < 0) {
  1692. dev_err(chip->dev, "set EN_DIV2_CP failed, rc=%d\n", rc);
  1693. return rc;
  1694. }
  1695. rc = smb1398_masked_write(chip, MISC_CFG1_REG, MISC_CFG1_MASK, 0x02);
  1696. if (rc < 0) {
  1697. dev_err(chip->dev, "set OP_MODE_COMBO failed, rc=%d\n", rc);
  1698. return rc;
  1699. }
  1700. /* Do not disable FP_FET during IREV conditions */
  1701. rc = smb1398_masked_write(chip, MISC_CFG0_REG, CFG_DIS_FPF_IREV_BIT, 0);
  1702. if (rc < 0) {
  1703. dev_err(chip->dev, "Couldn't set CFG_DIS_FPF_IREV_BIT, rc=%d\n",
  1704. rc);
  1705. return rc;
  1706. }
  1707. /* switcher enable controlled by register */
  1708. rc = smb1398_masked_write(chip, MISC_CFG0_REG,
  1709. SW_EN_SWITCHER_BIT, SW_EN_SWITCHER_BIT);
  1710. if (rc < 0) {
  1711. dev_err(chip->dev, "Couldn't set CFG_EN_SOURCE, rc=%d\n",
  1712. rc);
  1713. return rc;
  1714. }
  1715. if (IS_SMB1394(chip->div2_cp_role)) {
  1716. rc = smb1398_masked_write(chip, PERPH0_SSUPPLY_CFG0_REG,
  1717. CFG_CMP_VOUT_VS_4V_REF_MASK,
  1718. CMP_VOUT_VS_4V_REF_3P2V);
  1719. if (rc < 0) {
  1720. dev_err(chip->dev, "Couldn't set PERPH0_SSUPPLY_CFG0_REG, rc=%d\n",
  1721. rc);
  1722. return rc;
  1723. }
  1724. }
  1725. return rc;
  1726. }
  1727. #define DIV2_CP_MIN_ILIM_UA 1000000
  1728. static int smb1398_div2_cp_parse_dt(struct smb1398_chip *chip)
  1729. {
  1730. int rc = 0;
  1731. rc = of_property_match_string(chip->dev->of_node,
  1732. "io-channel-names", "die_temp");
  1733. if (rc < 0) {
  1734. dev_err(chip->dev, "die_temp IIO channel not found\n");
  1735. return rc;
  1736. }
  1737. chip->die_temp_chan = devm_iio_channel_get(chip->dev,
  1738. "die_temp");
  1739. if (IS_ERR(chip->die_temp_chan)) {
  1740. rc = PTR_ERR(chip->die_temp_chan);
  1741. if (rc != -EPROBE_DEFER)
  1742. dev_err(chip->dev, "Couldn't get die_temp_chan, rc=%d\n",
  1743. rc);
  1744. chip->die_temp_chan = NULL;
  1745. return rc;
  1746. }
  1747. of_property_read_u32(chip->dev->of_node, "qcom,div2-cp-min-ilim-ua",
  1748. &chip->div2_cp_min_ilim_ua);
  1749. /*
  1750. * Set minimum allowed ilim configuration to 1A for DIV2_CP
  1751. * operation.
  1752. */
  1753. if (chip->div2_cp_min_ilim_ua < DIV2_CP_MIN_ILIM_UA)
  1754. chip->div2_cp_min_ilim_ua = DIV2_CP_MIN_ILIM_UA;
  1755. chip->max_cutoff_soc = 85;
  1756. of_property_read_u32(chip->dev->of_node, "qcom,max-cutoff-soc",
  1757. &chip->max_cutoff_soc);
  1758. chip->ilim_ua_disable_div2_cp_slave = chip->div2_cp_min_ilim_ua * 3;
  1759. of_property_read_u32(chip->dev->of_node, "qcom,ilim-ua-disable-slave",
  1760. &chip->ilim_ua_disable_div2_cp_slave);
  1761. chip->cc_mode_taper_main_icl_ua = CC_MODE_TAPER_MAIN_ICL_UA;
  1762. of_property_read_u32(chip->dev->of_node,
  1763. "qcom,cc-mode-taper-main-icl-ua",
  1764. &chip->cc_mode_taper_main_icl_ua);
  1765. /* Default parallel output configuration is VPH connection */
  1766. chip->pl_output_mode = QTI_POWER_SUPPLY_PL_OUTPUT_VPH;
  1767. of_property_read_u32(chip->dev->of_node, "qcom,parallel-output-mode",
  1768. &chip->pl_output_mode);
  1769. /* Default parallel input configuration is USBMID connection */
  1770. chip->pl_input_mode = QTI_POWER_SUPPLY_PL_USBMID_USBMID;
  1771. of_property_read_u32(chip->dev->of_node, "qcom,parallel-input-mode",
  1772. &chip->pl_input_mode);
  1773. return 0;
  1774. }
  1775. static int smb1398_div2_cp_master_probe(struct smb1398_chip *chip)
  1776. {
  1777. int rc;
  1778. rc = smb1398_read(chip, REVID_REVISION4, &chip->rev4);
  1779. if (rc < 0) {
  1780. dev_err(chip->dev,
  1781. "Couldn't read REVID_REVISION4 rc=%d\n", rc);
  1782. return rc;
  1783. }
  1784. spin_lock_init(&chip->status_change_lock);
  1785. mutex_init(&chip->die_chan_lock);
  1786. rc = smb1398_div2_cp_parse_dt(chip);
  1787. if (rc < 0) {
  1788. dev_err(chip->dev, "Couldn't parse devicetree, rc=%d\n", rc);
  1789. return rc;
  1790. }
  1791. INIT_WORK(&chip->status_change_work, &smb1398_status_change_work);
  1792. INIT_WORK(&chip->taper_work, &smb1398_taper_work);
  1793. rc = smb1398_div2_cp_hw_init(chip);
  1794. if (rc < 0) {
  1795. dev_err(chip->dev, "div2_cp_hw_init failed, rc=%d\n", rc);
  1796. return rc;
  1797. }
  1798. rc = smb1398_div2_cp_create_votables(chip);
  1799. if (rc < 0) {
  1800. dev_err(chip->dev, "smb1398_div2_cp_create_votables failed, rc=%d\n",
  1801. rc);
  1802. return rc;
  1803. }
  1804. rc = smb1398_init_div2_cp_master_psy(chip);
  1805. if (rc > 0) {
  1806. dev_err(chip->dev, "smb1398_init_div2_cp_master_psy failed, rc=%d\n",
  1807. rc);
  1808. goto destroy_votable;
  1809. }
  1810. chip->nb.notifier_call = smb1398_notifier_cb;
  1811. rc = power_supply_reg_notifier(&chip->nb);
  1812. if (rc < 0) {
  1813. dev_err(chip->dev, "register notifier_cb failed, rc=%d\n", rc);
  1814. goto destroy_votable;
  1815. }
  1816. rc = smb1398_request_interrupts(chip);
  1817. if (rc < 0) {
  1818. dev_err(chip->dev, "smb1398_request_interrupts failed, rc=%d\n",
  1819. rc);
  1820. goto destroy_votable;
  1821. }
  1822. rc = device_init_wakeup(chip->dev, true);
  1823. if (rc < 0) {
  1824. dev_err(chip->dev, "init wakeup failed for div2_cp_master device, rc=%d\n",
  1825. rc);
  1826. return rc;
  1827. }
  1828. dev_dbg(chip->dev, "smb1398 DIV2_CP master is probed successfully\n");
  1829. return 0;
  1830. destroy_votable:
  1831. mutex_destroy(&chip->die_chan_lock);
  1832. smb1398_destroy_votables(chip);
  1833. return rc;
  1834. }
  1835. static enum power_supply_property div2_cp_slave_props[] = {
  1836. POWER_SUPPLY_PROP_MODEL_NAME,
  1837. };
  1838. static int div2_cp_slave_get_prop(struct power_supply *psy,
  1839. enum power_supply_property prop,
  1840. union power_supply_propval *val)
  1841. {
  1842. struct smb1398_chip *chip = power_supply_get_drvdata(psy);
  1843. switch (prop) {
  1844. case POWER_SUPPLY_PROP_MODEL_NAME:
  1845. val->strval = div2_cp_get_model_name(chip);
  1846. break;
  1847. default:
  1848. dev_err(chip->dev, "read div2_cp_slave property %d is not supported\n",
  1849. prop);
  1850. return -EINVAL;
  1851. }
  1852. return 0;
  1853. }
  1854. static const struct power_supply_desc div2_cps_psy_desc = {
  1855. .name = "cp_slave",
  1856. .type = POWER_SUPPLY_TYPE_MAINS,
  1857. .properties = div2_cp_slave_props,
  1858. .num_properties = ARRAY_SIZE(div2_cp_slave_props),
  1859. .get_property = div2_cp_slave_get_prop,
  1860. };
  1861. static int smb1398_init_div2_cp_slave_psy(struct smb1398_chip *chip)
  1862. {
  1863. int rc = 0;
  1864. struct power_supply_config cps_cfg = {};
  1865. cps_cfg.drv_data = chip;
  1866. cps_cfg.of_node = chip->dev->of_node;
  1867. chip->div2_cp_slave_psy = devm_power_supply_register(chip->dev,
  1868. &div2_cps_psy_desc, &cps_cfg);
  1869. if (IS_ERR(chip->div2_cp_slave_psy)) {
  1870. rc = PTR_ERR(chip->div2_cp_slave_psy);
  1871. dev_err(chip->dev, "register div2_cp_slave_psy failed, rc=%d\n",
  1872. rc);
  1873. return rc;
  1874. }
  1875. return 0;
  1876. }
  1877. static int smb1398_div2_cp_slave_probe(struct smb1398_chip *chip)
  1878. {
  1879. int rc;
  1880. u8 status;
  1881. rc = smb1398_read(chip, REVID_REVISION4, &chip->rev4);
  1882. if (rc < 0) {
  1883. dev_err(chip->dev,
  1884. "Couldn't read REVID_REVISION4 rc=%d\n", rc);
  1885. return rc;
  1886. }
  1887. rc = smb1398_update_ovp(chip);
  1888. if (rc < 0) {
  1889. dev_err(chip->dev, "Couldn't update OVP threshold rc=%d\n", rc);
  1890. return rc;
  1891. }
  1892. rc = smb1398_read(chip, MODE_STATUS_REG, &status);
  1893. if (rc < 0) {
  1894. dev_err(chip->dev, "Couldn't read slave MODE_STATUS_REG, rc=%d\n",
  1895. rc);
  1896. return rc;
  1897. }
  1898. /* Configure window (Vin/2 - Vout) UV level to 10mV */
  1899. rc = smb1398_masked_write(chip, NOLOCK_SPARE_REG,
  1900. DIV2_WIN_UV_SEL_BIT, 0);
  1901. if (rc < 0) {
  1902. dev_err(chip->dev, "Couldn't set WIN_UV_10_MV rc=%d\n", rc);
  1903. return rc;
  1904. }
  1905. /*
  1906. * Disable slave WIN_UV detection, otherwise slave might not be
  1907. * enabled due to WIN_UV until master drawing very high current.
  1908. */
  1909. rc = smb1398_masked_write(chip, PERPH0_CFG_SDCDC_REG, EN_WIN_UV_BIT, 0);
  1910. if (rc < 0) {
  1911. dev_err(chip->dev, "Couldn't disable DIV2_CP WIN_UV, rc=%d\n",
  1912. rc);
  1913. return rc;
  1914. }
  1915. /* Configure slave TEMP pin to HIGH-Z by default */
  1916. rc = smb1398_masked_write(chip, SSUPLY_TEMP_CTRL_REG,
  1917. SEL_OUT_TEMP_MAX_MASK, SEL_OUT_HIGHZ);
  1918. if (rc < 0) {
  1919. dev_err(chip->dev, "Couldn't set SSUPLY_TEMP_CTRL_REG, rc=%d\n",
  1920. rc);
  1921. return rc;
  1922. }
  1923. /* Configure to use Vtemp */
  1924. rc = smb1398_masked_write(chip, PERPH0_MISC_CFG2_REG,
  1925. CFG_TEMP_PIN_ITEMP, 0);
  1926. if (rc < 0) {
  1927. dev_err(chip->dev, "Couldn't set PERPH0_MISC_CFG2_REG, rc=%d\n",
  1928. rc);
  1929. return rc;
  1930. }
  1931. /* switcher enable controlled by register */
  1932. rc = smb1398_masked_write(chip, MISC_CFG0_REG,
  1933. SW_EN_SWITCHER_BIT, SW_EN_SWITCHER_BIT);
  1934. if (rc < 0) {
  1935. dev_err(chip->dev, "Couldn't set MISC_CFG0_REG, rc=%d\n",
  1936. rc);
  1937. return rc;
  1938. }
  1939. if (IS_SMB1394(chip->div2_cp_role)) {
  1940. rc = smb1398_masked_write(chip, PERPH0_SSUPPLY_CFG0_REG,
  1941. CFG_CMP_VOUT_VS_4V_REF_MASK,
  1942. CMP_VOUT_VS_4V_REF_3P2V);
  1943. if (rc < 0) {
  1944. dev_err(chip->dev, "Couldn't set PERPH0_SSUPPLY_CFG0_REG, rc=%d\n",
  1945. rc);
  1946. return rc;
  1947. }
  1948. rc = smb1398_masked_write(chip, PERPH0_DIV2_SLAVE,
  1949. CFG_EN_SLAVE_OWN_FREQ, CFG_EN_SLAVE_OWN_FREQ);
  1950. if (rc < 0) {
  1951. dev_err(chip->dev, "Couldn't set PERPH0_DIV2_SLAVE, rc=%d\n",
  1952. rc);
  1953. return rc;
  1954. }
  1955. } else {
  1956. /* Enable slave clock on its own */
  1957. rc = smb1398_masked_write(chip, NOLOCK_SPARE_REG,
  1958. EN_SLAVE_OWN_FREQ_BIT, EN_SLAVE_OWN_FREQ_BIT);
  1959. if (rc < 0) {
  1960. dev_err(chip->dev, "Couldn't enable slave clock, rc=%d\n",
  1961. rc);
  1962. return rc;
  1963. }
  1964. }
  1965. rc = smb1398_init_div2_cp_slave_psy(chip);
  1966. if (rc < 0) {
  1967. dev_err(chip->dev, "Initial div2_cp_slave_psy failed, rc=%d\n",
  1968. rc);
  1969. return rc;
  1970. }
  1971. dev_dbg(chip->dev, "smb1398 DIV2_CP slave probe successfully\n");
  1972. return 0;
  1973. }
  1974. static int smb1398_pre_regulator_iout_vote_cb(struct votable *votable,
  1975. void *data, int iout_ua, const char *client)
  1976. {
  1977. struct smb1398_chip *chip = (struct smb1398_chip *)data;
  1978. int rc = 0;
  1979. if (chip->in_suspend)
  1980. return -EAGAIN;
  1981. if (!client)
  1982. return -EINVAL;
  1983. iout_ua = min(iout_ua, MAX_IOUT_UA);
  1984. rc = smb1398_set_ichg_ma(chip, (iout_ua / 1000));
  1985. if (rc < 0)
  1986. return rc;
  1987. dev_dbg(chip->dev, "set iout %duA\n", iout_ua);
  1988. return 0;
  1989. }
  1990. static int smb1398_pre_regulator_vout_vote_cb(struct votable *votable,
  1991. void *data, int vout_uv, const char *client)
  1992. {
  1993. struct smb1398_chip *chip = (struct smb1398_chip *)data;
  1994. int rc = 0;
  1995. if (chip->in_suspend)
  1996. return -EAGAIN;
  1997. if (!client)
  1998. return -EINVAL;
  1999. vout_uv = min(vout_uv, MAX_1S_VOUT_UV);
  2000. rc = smb1398_set_1s_vout_mv(chip, vout_uv / 1000);
  2001. if (rc < 0)
  2002. return rc;
  2003. dev_dbg(chip->dev, "set vout %duV\n", vout_uv);
  2004. return 0;
  2005. }
  2006. static int smb1398_create_pre_regulator_votables(struct smb1398_chip *chip)
  2007. {
  2008. chip->pre_regulator_iout_votable = create_votable("PRE_REGULATOR_IOUT",
  2009. VOTE_MIN, smb1398_pre_regulator_iout_vote_cb, chip);
  2010. if (IS_ERR_OR_NULL(chip->pre_regulator_iout_votable))
  2011. return PTR_ERR_OR_ZERO(chip->pre_regulator_iout_votable);
  2012. chip->pre_regulator_vout_votable = create_votable("PRE_REGULATOR_VOUT",
  2013. VOTE_MIN, smb1398_pre_regulator_vout_vote_cb, chip);
  2014. if (IS_ERR_OR_NULL(chip->pre_regulator_vout_votable)) {
  2015. destroy_votable(chip->pre_regulator_iout_votable);
  2016. return PTR_ERR_OR_ZERO(chip->pre_regulator_vout_votable);
  2017. }
  2018. return 0;
  2019. }
  2020. static enum power_supply_property pre_regulator_props[] = {
  2021. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  2022. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  2023. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  2024. };
  2025. static int pre_regulator_get_prop(struct power_supply *psy,
  2026. enum power_supply_property prop,
  2027. union power_supply_propval *pval)
  2028. {
  2029. struct smb1398_chip *chip = power_supply_get_drvdata(psy);
  2030. int rc, iin_ma, iout_ma, vout_mv;
  2031. switch (prop) {
  2032. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  2033. rc = smb1398_get_iin_ma(chip, &iin_ma);
  2034. if (rc < 0)
  2035. return rc;
  2036. pval->intval = iin_ma * 1000;
  2037. break;
  2038. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  2039. if (chip->pre_regulator_iout_votable) {
  2040. pval->intval = get_effective_result(
  2041. chip->pre_regulator_iout_votable);
  2042. } else {
  2043. rc = smb1398_get_ichg_ma(chip, &iout_ma);
  2044. if (rc < 0)
  2045. return rc;
  2046. pval->intval = iout_ma * 1000;
  2047. }
  2048. break;
  2049. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  2050. if (chip->pre_regulator_vout_votable) {
  2051. pval->intval = get_effective_result(
  2052. chip->pre_regulator_vout_votable);
  2053. } else {
  2054. rc = smb1398_get_1s_vout_mv(chip, &vout_mv);
  2055. if (rc < 0)
  2056. return rc;
  2057. pval->intval = vout_mv * 1000;
  2058. }
  2059. break;
  2060. default:
  2061. dev_err(chip->dev, "read pre_regulator property %d is not supported\n",
  2062. prop);
  2063. return -EINVAL;
  2064. }
  2065. return 0;
  2066. }
  2067. static int pre_regulator_set_prop(struct power_supply *psy,
  2068. enum power_supply_property prop,
  2069. const union power_supply_propval *pval)
  2070. {
  2071. struct smb1398_chip *chip = power_supply_get_drvdata(psy);
  2072. int rc = 0;
  2073. switch (prop) {
  2074. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  2075. rc = smb1398_set_iin_ma(chip, pval->intval / 1000);
  2076. if (rc < 0)
  2077. return rc;
  2078. break;
  2079. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  2080. vote(chip->pre_regulator_iout_votable, CP_VOTER,
  2081. true, pval->intval);
  2082. break;
  2083. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  2084. vote(chip->pre_regulator_vout_votable, CP_VOTER,
  2085. true, pval->intval);
  2086. break;
  2087. default:
  2088. dev_err(chip->dev, "write pre_regulator property %d is not supported\n",
  2089. prop);
  2090. return -EINVAL;
  2091. }
  2092. return rc;
  2093. }
  2094. static int pre_regulator_is_writeable(struct power_supply *psy,
  2095. enum power_supply_property prop)
  2096. {
  2097. switch (prop) {
  2098. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  2099. case POWER_SUPPLY_PROP_CURRENT_MAX:
  2100. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  2101. return 1;
  2102. default:
  2103. break;
  2104. }
  2105. return 0;
  2106. }
  2107. static const struct power_supply_desc pre_regulator_psy_desc = {
  2108. .name = "pre_regulator",
  2109. .type = POWER_SUPPLY_TYPE_MAINS,
  2110. .properties = pre_regulator_props,
  2111. .num_properties = ARRAY_SIZE(pre_regulator_props),
  2112. .get_property = pre_regulator_get_prop,
  2113. .set_property = pre_regulator_set_prop,
  2114. .property_is_writeable = pre_regulator_is_writeable,
  2115. };
  2116. static int smb1398_create_pre_regulator_psy(struct smb1398_chip *chip)
  2117. {
  2118. struct power_supply_config pre_regulator_psy_cfg = {};
  2119. int rc = 0;
  2120. pre_regulator_psy_cfg.drv_data = chip;
  2121. pre_regulator_psy_cfg.of_node = chip->dev->of_node;
  2122. chip->pre_regulator_psy = devm_power_supply_register(chip->dev,
  2123. &pre_regulator_psy_desc,
  2124. &pre_regulator_psy_cfg);
  2125. if (IS_ERR(chip->pre_regulator_psy)) {
  2126. rc = PTR_ERR(chip->pre_regulator_psy);
  2127. dev_err(chip->dev, "register pre_regulator psy failed, rc=%d\n",
  2128. rc);
  2129. return rc;
  2130. }
  2131. return 0;
  2132. }
  2133. static int smb1398_pre_regulator_probe(struct smb1398_chip *chip)
  2134. {
  2135. int rc = 0;
  2136. rc = smb1398_create_pre_regulator_votables(chip);
  2137. if (rc < 0) {
  2138. dev_err(chip->dev, "Create votable for pre_regulator failed, rc=%d\n",
  2139. rc);
  2140. return rc;
  2141. }
  2142. rc = smb1398_create_pre_regulator_psy(chip);
  2143. if (rc < 0) {
  2144. dev_err(chip->dev, "Create pre-regulator failed, rc=%d\n",
  2145. rc);
  2146. return rc;
  2147. }
  2148. return 0;
  2149. }
  2150. /* master IIO configuration */
  2151. static const struct cp_iio_prop_channels cp_master_chans[] = {
  2152. SMB1398_CHAN_INDEX("cp_master_cp_status_1", PSY_IIO_CP_STATUS1)
  2153. SMB1398_CHAN_INDEX("cp_master_cp_status_2", PSY_IIO_CP_STATUS2)
  2154. SMB1398_CHAN_INDEX("cp_master_cp_enable", PSY_IIO_CP_ENABLE)
  2155. SMB1398_CHAN_INDEX("cp_master_cp_switcher_en", PSY_IIO_CP_SWITCHER_EN)
  2156. SMB1398_CHAN_TEMP("cp_master_cp_die_temp", PSY_IIO_CP_DIE_TEMP)
  2157. SMB1398_CHAN_CUR("cp_master_cp_isns", PSY_IIO_CP_ISNS)
  2158. SMB1398_CHAN_CUR("cp_master_cp_isns_slave", PSY_IIO_CP_ISNS_SLAVE)
  2159. SMB1398_CHAN_INDEX("cp_master_cp_toggle_switcher",
  2160. PSY_IIO_CP_TOGGLE_SWITCHER)
  2161. SMB1398_CHAN_INDEX("cp_master_cp_irq_status", PSY_IIO_IRQ_STATUS)
  2162. SMB1398_CHAN_CUR("cp_master_cp_ilim", PSY_IIO_CP_ILIM)
  2163. SMB1398_CHAN_INDEX("cp_master_chip_version", PSY_IIO_CHIP_VERSION)
  2164. SMB1398_CHAN_INDEX("cp_master_parallel_mode", PSY_IIO_PARALLEL_MODE)
  2165. SMB1398_CHAN_INDEX("cp_master_parallel_output_mode",
  2166. PSY_IIO_PARALLEL_OUTPUT_MODE)
  2167. SMB1398_CHAN_CUR("cp_master_min_icl", PSY_IIO_MIN_ICL)
  2168. };
  2169. static int cp_master_iio_set_prop(struct smb1398_chip *chip,
  2170. int channel, int val)
  2171. {
  2172. switch (channel) {
  2173. case PSY_IIO_CP_ENABLE:
  2174. vote(chip->div2_cp_disable_votable,
  2175. USER_VOTER, !val, 0);
  2176. break;
  2177. case PSY_IIO_CP_TOGGLE_SWITCHER:
  2178. if (!!val)
  2179. smb1398_toggle_switcher(chip);
  2180. break;
  2181. case PSY_IIO_IRQ_STATUS:
  2182. chip->div2_irq_status = val;
  2183. break;
  2184. case PSY_IIO_CP_ILIM:
  2185. if (chip->div2_cp_ilim_votable)
  2186. vote_override(chip->div2_cp_ilim_votable, CC_MODE_VOTER,
  2187. (val > 0), val);
  2188. break;
  2189. default:
  2190. pr_err("get prop %d is not supported\n", channel);
  2191. return -EINVAL;
  2192. }
  2193. return 0;
  2194. }
  2195. static int cp_master_iio_get_prop_in_suspend(struct smb1398_chip *chip,
  2196. int channel, int *val)
  2197. {
  2198. switch (channel) {
  2199. case PSY_IIO_CP_STATUS1:
  2200. *val = chip->cp_status1;
  2201. break;
  2202. case PSY_IIO_CP_STATUS2:
  2203. *val = chip->cp_status2;
  2204. break;
  2205. case PSY_IIO_CP_ENABLE:
  2206. *val = chip->cp_enable;
  2207. break;
  2208. case PSY_IIO_CP_SWITCHER_EN:
  2209. *val = chip->switcher_en;
  2210. break;
  2211. case PSY_IIO_CP_DIE_TEMP:
  2212. *val = chip->die_temp;
  2213. break;
  2214. case PSY_IIO_CP_ISNS:
  2215. *val = chip->cp_isns_master;
  2216. break;
  2217. case PSY_IIO_CP_ISNS_SLAVE:
  2218. *val = chip->cp_isns_slave;
  2219. break;
  2220. case PSY_IIO_IRQ_STATUS:
  2221. *val = chip->div2_irq_status;
  2222. break;
  2223. case PSY_IIO_CP_ILIM:
  2224. *val = chip->cp_ilim;
  2225. break;
  2226. default:
  2227. return -EINVAL;
  2228. }
  2229. return 0;
  2230. }
  2231. static int cp_master_iio_get_prop(struct smb1398_chip *chip,
  2232. int channel, int *val)
  2233. {
  2234. int rc = 0, temp, isns_ua, ilim_ma;
  2235. u8 status;
  2236. /*
  2237. * Return the cached values when the system is in suspend state
  2238. * instead of reading the registers to avoid read failures.
  2239. */
  2240. if (chip->in_suspend) {
  2241. rc = cp_master_iio_get_prop_in_suspend(chip, channel, val);
  2242. if (!rc)
  2243. return IIO_VAL_INT;
  2244. rc = 0;
  2245. }
  2246. switch (channel) {
  2247. case PSY_IIO_CP_STATUS1:
  2248. rc = smb1398_div2_cp_get_status1(chip, &status);
  2249. if (!rc)
  2250. chip->cp_status1 = *val = status;
  2251. break;
  2252. case PSY_IIO_CP_STATUS2:
  2253. rc = smb1398_div2_cp_get_status2(chip, &status);
  2254. if (!rc)
  2255. chip->cp_status2 = *val = status;
  2256. break;
  2257. case PSY_IIO_CP_ENABLE:
  2258. rc = smb1398_get_enable_status(chip);
  2259. if (!rc)
  2260. chip->cp_enable = *val = chip->smb_en &&
  2261. !get_effective_result(
  2262. chip->div2_cp_disable_votable);
  2263. break;
  2264. case PSY_IIO_CP_SWITCHER_EN:
  2265. rc = smb1398_get_enable_status(chip);
  2266. if (!rc)
  2267. *val = chip->switcher_en;
  2268. break;
  2269. case PSY_IIO_CP_ISNS:
  2270. rc = smb1398_div2_cp_get_master_isns(chip, &isns_ua);
  2271. if (rc >= 0)
  2272. chip->cp_isns_master = *val = isns_ua;
  2273. break;
  2274. case PSY_IIO_CP_ISNS_SLAVE:
  2275. rc = smb1398_div2_cp_get_slave_isns(chip, &isns_ua);
  2276. if (rc >= 0)
  2277. chip->cp_isns_slave = *val = isns_ua;
  2278. break;
  2279. case PSY_IIO_CP_TOGGLE_SWITCHER:
  2280. *val = 0;
  2281. break;
  2282. case PSY_IIO_CP_DIE_TEMP:
  2283. rc = smb1398_get_die_temp(chip, &temp);
  2284. if (rc >= 0) {
  2285. *val = temp;
  2286. if (temp <= THERMAL_SUSPEND_DECIDEGC)
  2287. chip->die_temp = temp;
  2288. else if (chip->die_temp == -ENODATA)
  2289. rc = -ENODATA;
  2290. else
  2291. *val = chip->die_temp;
  2292. }
  2293. break;
  2294. case PSY_IIO_IRQ_STATUS:
  2295. *val = chip->div2_irq_status;
  2296. rc = smb1398_div2_cp_get_irq_status(chip, &status);
  2297. if (!rc)
  2298. *val |= status;
  2299. break;
  2300. case PSY_IIO_CP_ILIM:
  2301. if (is_cps_available(chip)) {
  2302. if (chip->div2_cp_ilim_votable)
  2303. *val = get_effective_result(
  2304. chip->div2_cp_ilim_votable);
  2305. } else {
  2306. rc = smb1398_get_iin_ma(chip, &ilim_ma);
  2307. if (!rc)
  2308. *val = (ilim_ma * 1000 * 100)
  2309. / DIV2_ILIM_CFG_PCT;
  2310. }
  2311. chip->cp_ilim = *val;
  2312. break;
  2313. case PSY_IIO_CHIP_VERSION:
  2314. *val = chip->rev4;
  2315. break;
  2316. case PSY_IIO_PARALLEL_MODE:
  2317. *val = chip->pl_input_mode;
  2318. break;
  2319. case PSY_IIO_PARALLEL_OUTPUT_MODE:
  2320. *val = chip->pl_output_mode;
  2321. break;
  2322. case PSY_IIO_MIN_ICL:
  2323. *val = smb1398_div2_cp_get_min_icl(chip);
  2324. break;
  2325. default:
  2326. pr_err("get prop %d is not supported\n", channel);
  2327. rc = -EINVAL;
  2328. break;
  2329. }
  2330. if (rc < 0 && rc != -ENODATA) {
  2331. pr_err("Couldn't get prop %d rc = %d\n", channel, rc);
  2332. return rc;
  2333. }
  2334. return IIO_VAL_INT;
  2335. }
  2336. static int cp_master_write_raw(struct iio_dev *indio_dev,
  2337. struct iio_chan_spec const *chan, int val, int val2,
  2338. long mask)
  2339. {
  2340. struct smb1398_chip *iio_chip = iio_priv(indio_dev);
  2341. int channel;
  2342. channel = chan->channel;
  2343. return cp_master_iio_set_prop(iio_chip, channel, val);
  2344. }
  2345. static int cp_master_read_raw(struct iio_dev *indio_dev,
  2346. struct iio_chan_spec const *chan, int *val, int *val2,
  2347. long mask)
  2348. {
  2349. struct smb1398_chip *iio_chip = iio_priv(indio_dev);
  2350. int channel;
  2351. channel = chan->channel;
  2352. return cp_master_iio_get_prop(iio_chip, channel, val);
  2353. }
  2354. /* slave IIO configuration */
  2355. static const struct cp_iio_prop_channels cp_slave_chans[] = {
  2356. SMB1398_CHAN_INDEX("cp_slave_cp_enable", PSY_IIO_CP_ENABLE)
  2357. SMB1398_CHAN_CUR("cp_slave_input_current_max",
  2358. PSY_IIO_CP_INPUT_CURRENT_MAX)
  2359. SMB1398_CHAN_CUR("cp_slave_current_capability",
  2360. PSY_IIO_CURRENT_CAPABILITY)
  2361. };
  2362. static int cp_slave_iio_set_prop(struct smb1398_chip *chip,
  2363. int channel, int val)
  2364. {
  2365. int ilim_ma, rc = 0;
  2366. enum isns_mode mode;
  2367. switch (channel) {
  2368. case PSY_IIO_CP_ENABLE:
  2369. rc = smb1398_div2_cp_switcher_en(chip, !!val);
  2370. break;
  2371. case PSY_IIO_CP_INPUT_CURRENT_MAX:
  2372. ilim_ma = val / 1000;
  2373. rc = smb1398_set_iin_ma(chip, ilim_ma);
  2374. break;
  2375. case PSY_IIO_CURRENT_CAPABILITY:
  2376. mode = (enum isns_mode)val;
  2377. rc = smb1398_div2_cp_isns_mode_control(chip, mode);
  2378. if (rc < 0)
  2379. return rc;
  2380. chip->current_capability = mode;
  2381. break;
  2382. default:
  2383. pr_err("get prop %d is not supported\n", channel);
  2384. rc = -EINVAL;
  2385. break;
  2386. }
  2387. if (rc < 0) {
  2388. pr_err("Couldn't set prop %d rc = %d\n", channel, rc);
  2389. return rc;
  2390. }
  2391. return 0;
  2392. }
  2393. static int cp_slave_iio_get_prop(struct smb1398_chip *chip,
  2394. int channel, int *val)
  2395. {
  2396. switch (channel) {
  2397. case PSY_IIO_CP_ENABLE:
  2398. *val = chip->switcher_en;
  2399. break;
  2400. case PSY_IIO_CP_INPUT_CURRENT_MAX:
  2401. *val = 0;
  2402. if (!chip->div2_cp_ilim_votable)
  2403. chip->div2_cp_ilim_votable = find_votable("CP_ILIM");
  2404. if (chip->div2_cp_ilim_votable)
  2405. *val = get_effective_result_locked(
  2406. chip->div2_cp_ilim_votable);
  2407. break;
  2408. case PSY_IIO_CURRENT_CAPABILITY:
  2409. *val = (int)chip->current_capability;
  2410. break;
  2411. default:
  2412. pr_err("get prop %d is not supported\n", channel);
  2413. return -EINVAL;
  2414. }
  2415. return IIO_VAL_INT;
  2416. }
  2417. static int cp_slave_write_raw(struct iio_dev *indio_dev,
  2418. struct iio_chan_spec const *chan, int val, int val2,
  2419. long mask)
  2420. {
  2421. struct smb1398_chip *iio_chip = iio_priv(indio_dev);
  2422. int channel;
  2423. channel = chan->channel;
  2424. return cp_slave_iio_set_prop(iio_chip, channel, val);
  2425. }
  2426. static int cp_slave_read_raw(struct iio_dev *indio_dev,
  2427. struct iio_chan_spec const *chan, int *val, int *val2,
  2428. long mask)
  2429. {
  2430. struct smb1398_chip *iio_chip = iio_priv(indio_dev);
  2431. int channel;
  2432. channel = chan->channel;
  2433. return cp_slave_iio_get_prop(iio_chip, channel, val);
  2434. }
  2435. static int cp_fwnode_xlate(struct iio_dev *indio_dev,
  2436. const struct fwnode_reference_args *iiospec)
  2437. {
  2438. struct smb1398_chip *iio_chip = iio_priv(indio_dev);
  2439. int i;
  2440. struct iio_chan_spec *iio_chan = iio_chip->cp_iio_chan_ids;
  2441. for (i = 0; i < iio_chip->nchannels; i++) {
  2442. if (iio_chan->channel == iiospec->args[0])
  2443. return i;
  2444. iio_chan++;
  2445. }
  2446. return -EINVAL;
  2447. }
  2448. static const struct iio_info cp_master_iio_info = {
  2449. .read_raw = cp_master_read_raw,
  2450. .write_raw = cp_master_write_raw,
  2451. .fwnode_xlate = cp_fwnode_xlate,
  2452. };
  2453. static const struct iio_info cp_slave_iio_info = {
  2454. .read_raw = cp_slave_read_raw,
  2455. .write_raw = cp_slave_write_raw,
  2456. .fwnode_xlate = cp_fwnode_xlate,
  2457. };
  2458. static int cp_smb5_iio_init(struct smb1398_chip *chip)
  2459. {
  2460. int rc = 0;
  2461. struct iio_channel **iio_list;
  2462. if (IS_ERR(chip->smb5_iio_chan_list))
  2463. return -EINVAL;
  2464. iio_list = get_ext_channels(chip->dev,
  2465. cp_smb5_ext_iio_chan, ARRAY_SIZE(cp_smb5_ext_iio_chan));
  2466. if (IS_ERR(iio_list)) {
  2467. rc = PTR_ERR(iio_list);
  2468. if (rc != -EPROBE_DEFER) {
  2469. dev_err(chip->dev, "Failed to get channels, rc=%d\n",
  2470. rc);
  2471. chip->smb5_iio_chan_list = ERR_PTR(-EINVAL);
  2472. }
  2473. return rc;
  2474. }
  2475. chip->smb5_iio_chan_list = iio_list;
  2476. return 0;
  2477. }
  2478. static int cp_iio_probe_init(struct smb1398_chip *chip,
  2479. struct iio_dev *indio_dev, const struct cp_iio_prop_channels *cp_chans,
  2480. const struct iio_info *cp_iio_info)
  2481. {
  2482. int i;
  2483. struct iio_chan_spec *iio_chan;
  2484. chip->cp_iio_chan_ids = devm_kcalloc(chip->dev, chip->nchannels,
  2485. sizeof(*chip->cp_iio_chan_ids), GFP_KERNEL);
  2486. if (!chip->cp_iio_chan_ids)
  2487. return -ENOMEM;
  2488. for (i = 0; i < chip->nchannels; i++) {
  2489. iio_chan = &chip->cp_iio_chan_ids[i];
  2490. iio_chan->channel = cp_chans[i].channel_no;
  2491. iio_chan->datasheet_name =
  2492. cp_chans[i].datasheet_name;
  2493. iio_chan->extend_name = cp_chans[i].datasheet_name;
  2494. iio_chan->info_mask_separate =
  2495. cp_chans[i].info_mask;
  2496. iio_chan->type = cp_chans[i].type;
  2497. iio_chan->address = i;
  2498. }
  2499. if (chip->div2_cp_role == DIV2_CP_MASTER ||
  2500. chip->div2_cp_role == SMB1394_DIV2_CP_PRY) {
  2501. cp_smb5_iio_init(chip);
  2502. indio_dev->name = "smb1396-div2-cp-master";
  2503. } else {
  2504. indio_dev->name = "smb1396-div2-cp-slave";
  2505. }
  2506. indio_dev->info = cp_iio_info;
  2507. return 0;
  2508. }
  2509. static int smb1398_probe(struct platform_device *pdev)
  2510. {
  2511. struct smb1398_chip *chip;
  2512. struct iio_dev *indio_dev;
  2513. int rc = 0;
  2514. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*chip));
  2515. if (!indio_dev)
  2516. return -ENOMEM;
  2517. chip = iio_priv(indio_dev);
  2518. chip->die_temp = -ENODATA;
  2519. chip->dev = &pdev->dev;
  2520. chip->regmap = dev_get_regmap(chip->dev->parent, NULL);
  2521. if (!chip->regmap) {
  2522. dev_err(chip->dev, "Get regmap failed\n");
  2523. return -EINVAL;
  2524. }
  2525. chip->disabled = true;
  2526. platform_set_drvdata(pdev, chip);
  2527. chip->div2_cp_role = (u32)(unsigned long)of_device_get_match_data(chip->dev);
  2528. switch (chip->div2_cp_role) {
  2529. case DIV2_CP_MASTER:
  2530. case SMB1394_DIV2_CP_PRY:
  2531. chip->nchannels = ARRAY_SIZE(cp_master_chans);
  2532. rc = smb1398_div2_cp_master_probe(chip);
  2533. if (rc < 0) {
  2534. if (rc != -EPROBE_DEFER)
  2535. dev_err(chip->dev, "Couldn't probe SMB1398 master rc= %d\n",
  2536. rc);
  2537. goto cleanup;
  2538. }
  2539. rc = cp_iio_probe_init(chip, indio_dev, cp_master_chans, &cp_master_iio_info);
  2540. break;
  2541. case DIV2_CP_SLAVE:
  2542. case SMB1394_DIV2_CP_SECY:
  2543. chip->nchannels = ARRAY_SIZE(cp_slave_chans);
  2544. rc = smb1398_div2_cp_slave_probe(chip);
  2545. if (rc < 0) {
  2546. if (rc != -EPROBE_DEFER)
  2547. dev_err(chip->dev, "Couldn't probe SMB1398 slave rc= %d\n",
  2548. rc);
  2549. goto cleanup;
  2550. }
  2551. rc = cp_iio_probe_init(chip, indio_dev, cp_slave_chans, &cp_slave_iio_info);
  2552. break;
  2553. case COMBO_PRE_REGULATOR:
  2554. rc = smb1398_pre_regulator_probe(chip);
  2555. break;
  2556. default:
  2557. dev_err(chip->dev, "Couldn't find a match role for %d\n",
  2558. chip->div2_cp_role);
  2559. goto cleanup;
  2560. }
  2561. if (rc < 0) {
  2562. if (rc != -EPROBE_DEFER)
  2563. dev_err(chip->dev, "IIO init failed for %s rc= %d\n",
  2564. !!chip->div2_cp_role ? "slave" : "master", rc);
  2565. goto cleanup;
  2566. }
  2567. /*
  2568. * This configuration below is applicable to both
  2569. * master and slave. The individual channel
  2570. * configurations are done in master/slave
  2571. * iio_probe_init calls.
  2572. */
  2573. indio_dev->dev.parent = &pdev->dev;
  2574. indio_dev->dev.of_node = pdev->dev.of_node;
  2575. indio_dev->modes = INDIO_DIRECT_MODE;
  2576. indio_dev->channels = chip->cp_iio_chan_ids;
  2577. indio_dev->num_channels = chip->nchannels;
  2578. rc = devm_iio_device_register(&pdev->dev, indio_dev);
  2579. if (rc) {
  2580. pr_err("iio device register failed rc=%d\n", rc);
  2581. goto cleanup;
  2582. }
  2583. return 0;
  2584. cleanup:
  2585. platform_set_drvdata(pdev, NULL);
  2586. return rc;
  2587. }
  2588. static int smb1398_remove(struct platform_device *pdev)
  2589. {
  2590. struct smb1398_chip *chip = platform_get_drvdata(pdev);
  2591. if (chip->div2_cp_role == DIV2_CP_MASTER ||
  2592. chip->div2_cp_role == SMB1394_DIV2_CP_PRY) {
  2593. vote(chip->awake_votable, SHUTDOWN_VOTER, false, 0);
  2594. vote(chip->div2_cp_disable_votable, SHUTDOWN_VOTER, true, 0);
  2595. vote(chip->div2_cp_ilim_votable, SHUTDOWN_VOTER, true, 0);
  2596. cancel_work_sync(&chip->taper_work);
  2597. cancel_work_sync(&chip->status_change_work);
  2598. mutex_destroy(&chip->die_chan_lock);
  2599. smb1398_destroy_votables(chip);
  2600. }
  2601. return 0;
  2602. }
  2603. static int smb1398_suspend(struct device *dev)
  2604. {
  2605. struct smb1398_chip *chip = dev_get_drvdata(dev);
  2606. chip->in_suspend = true;
  2607. return 0;
  2608. }
  2609. static int smb1398_resume(struct device *dev)
  2610. {
  2611. struct smb1398_chip *chip = dev_get_drvdata(dev);
  2612. chip->in_suspend = false;
  2613. if (chip->div2_cp_role == DIV2_CP_MASTER) {
  2614. rerun_election(chip->div2_cp_ilim_votable);
  2615. rerun_election(chip->div2_cp_disable_votable);
  2616. }
  2617. return 0;
  2618. }
  2619. static void smb1398_shutdown(struct platform_device *pdev)
  2620. {
  2621. struct smb1398_chip *chip = platform_get_drvdata(pdev);
  2622. int rc;
  2623. power_supply_unreg_notifier(&chip->nb);
  2624. /* Disable SMB1398 */
  2625. rc = smb1398_div2_cp_switcher_en(chip, 0);
  2626. if (rc < 0)
  2627. dev_err(chip->dev, "Couldn't disable chip rc= %d\n", rc);
  2628. rc = smb1398_toggle_uvlo(chip);
  2629. if (rc < 0)
  2630. dev_err(chip->dev, "Couldn't toggle uvlo rc= %d\n", rc);
  2631. }
  2632. static const struct dev_pm_ops smb1398_pm_ops = {
  2633. .suspend = smb1398_suspend,
  2634. .resume = smb1398_resume,
  2635. };
  2636. static const struct of_device_id match_table[] = {
  2637. { .compatible = "qcom,smb1396-div2-cp-master",
  2638. .data = (void *)DIV2_CP_MASTER,
  2639. },
  2640. { .compatible = "qcom,smb1396-div2-cp-slave",
  2641. .data = (void *)DIV2_CP_SLAVE,
  2642. },
  2643. { .compatible = "qcom,smb1398-pre-regulator",
  2644. .data = (void *)COMBO_PRE_REGULATOR,
  2645. },
  2646. { .compatible = "qcom,smb1394-div2-cp-primary",
  2647. .data = (void *)SMB1394_DIV2_CP_PRY,
  2648. },
  2649. { .compatible = "qcom,smb1394-div2-cp-secondary",
  2650. .data = (void *)SMB1394_DIV2_CP_SECY,
  2651. },
  2652. {
  2653. },
  2654. };
  2655. static struct platform_driver smb1398_driver = {
  2656. .driver = {
  2657. .name = "qcom,smb1398-charger",
  2658. .pm = &smb1398_pm_ops,
  2659. .of_match_table = match_table,
  2660. },
  2661. .probe = smb1398_probe,
  2662. .remove = smb1398_remove,
  2663. .shutdown = smb1398_shutdown,
  2664. };
  2665. module_platform_driver(smb1398_driver);
  2666. MODULE_DESCRIPTION("SMB1398 charger driver");
  2667. MODULE_LICENSE("GPL");