ab8500_charger.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2012
  4. *
  5. * Charger driver for AB8500
  6. *
  7. * Author:
  8. * Johan Palsson <johan.palsson@stericsson.com>
  9. * Karl Komierowski <karl.komierowski@stericsson.com>
  10. * Arun R Murthy <arun.murthy@stericsson.com>
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/notifier.h>
  18. #include <linux/slab.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/power_supply.h>
  21. #include <linux/completion.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/err.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/kobject.h>
  26. #include <linux/of.h>
  27. #include <linux/mfd/core.h>
  28. #include <linux/mfd/abx500/ab8500.h>
  29. #include <linux/mfd/abx500.h>
  30. #include <linux/mfd/abx500/ab8500-bm.h>
  31. #include <linux/mfd/abx500/ux500_chargalg.h>
  32. #include <linux/usb/otg.h>
  33. #include <linux/mutex.h>
  34. #include <linux/iio/consumer.h>
  35. /* Charger constants */
  36. #define NO_PW_CONN 0
  37. #define AC_PW_CONN 1
  38. #define USB_PW_CONN 2
  39. #define MAIN_WDOG_ENA 0x01
  40. #define MAIN_WDOG_KICK 0x02
  41. #define MAIN_WDOG_DIS 0x00
  42. #define CHARG_WD_KICK 0x01
  43. #define MAIN_CH_ENA 0x01
  44. #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
  45. #define USB_CH_ENA 0x01
  46. #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
  47. #define MAIN_CH_DET 0x01
  48. #define MAIN_CH_CV_ON 0x04
  49. #define USB_CH_CV_ON 0x08
  50. #define VBUS_DET_DBNC100 0x02
  51. #define VBUS_DET_DBNC1 0x01
  52. #define OTP_ENABLE_WD 0x01
  53. #define DROP_COUNT_RESET 0x01
  54. #define USB_CH_DET 0x01
  55. #define MAIN_CH_INPUT_CURR_SHIFT 4
  56. #define VBUS_IN_CURR_LIM_SHIFT 4
  57. #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
  58. #define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
  59. #define LED_INDICATOR_PWM_ENA 0x01
  60. #define LED_INDICATOR_PWM_DIS 0x00
  61. #define LED_IND_CUR_5MA 0x04
  62. #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
  63. /* HW failure constants */
  64. #define MAIN_CH_TH_PROT 0x02
  65. #define VBUS_CH_NOK 0x08
  66. #define USB_CH_TH_PROT 0x02
  67. #define VBUS_OVV_TH 0x01
  68. #define MAIN_CH_NOK 0x01
  69. #define VBUS_DET 0x80
  70. #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
  71. #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
  72. #define USB_CH_VBUSDROP 0x40
  73. #define USB_CH_VBUSDETDBNC 0x01
  74. /* UsbLineStatus register bit masks */
  75. #define AB8500_USB_LINK_STATUS 0x78
  76. #define AB8505_USB_LINK_STATUS 0xF8
  77. #define AB8500_STD_HOST_SUSP 0x18
  78. #define USB_LINK_STATUS_SHIFT 3
  79. /* Watchdog timeout constant */
  80. #define WD_TIMER 0x30 /* 4min */
  81. #define WD_KICK_INTERVAL (60 * HZ)
  82. /* Lowest charger voltage is 3.39V -> 0x4E */
  83. #define LOW_VOLT_REG 0x4E
  84. /* Step up/down delay in us */
  85. #define STEP_UDELAY 1000
  86. #define CHARGER_STATUS_POLL 10 /* in ms */
  87. #define CHG_WD_INTERVAL (60 * HZ)
  88. #define AB8500_SW_CONTROL_FALLBACK 0x03
  89. /* Wait for enumeration before charing in us */
  90. #define WAIT_ACA_RID_ENUMERATION (5 * 1000)
  91. /*External charger control*/
  92. #define AB8500_SYS_CHARGER_CONTROL_REG 0x52
  93. #define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
  94. #define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
  95. /* UsbLineStatus register - usb types */
  96. enum ab8500_charger_link_status {
  97. USB_STAT_NOT_CONFIGURED,
  98. USB_STAT_STD_HOST_NC,
  99. USB_STAT_STD_HOST_C_NS,
  100. USB_STAT_STD_HOST_C_S,
  101. USB_STAT_HOST_CHG_NM,
  102. USB_STAT_HOST_CHG_HS,
  103. USB_STAT_HOST_CHG_HS_CHIRP,
  104. USB_STAT_DEDICATED_CHG,
  105. USB_STAT_ACA_RID_A,
  106. USB_STAT_ACA_RID_B,
  107. USB_STAT_ACA_RID_C_NM,
  108. USB_STAT_ACA_RID_C_HS,
  109. USB_STAT_ACA_RID_C_HS_CHIRP,
  110. USB_STAT_HM_IDGND,
  111. USB_STAT_RESERVED,
  112. USB_STAT_NOT_VALID_LINK,
  113. USB_STAT_PHY_EN,
  114. USB_STAT_SUP_NO_IDGND_VBUS,
  115. USB_STAT_SUP_IDGND_VBUS,
  116. USB_STAT_CHARGER_LINE_1,
  117. USB_STAT_CARKIT_1,
  118. USB_STAT_CARKIT_2,
  119. USB_STAT_ACA_DOCK_CHARGER,
  120. };
  121. enum ab8500_usb_state {
  122. AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
  123. AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
  124. AB8500_BM_USB_STATE_CONFIGURED,
  125. AB8500_BM_USB_STATE_SUSPEND,
  126. AB8500_BM_USB_STATE_RESUME,
  127. AB8500_BM_USB_STATE_MAX,
  128. };
  129. /* VBUS input current limits supported in AB8500 in mA */
  130. #define USB_CH_IP_CUR_LVL_0P05 50
  131. #define USB_CH_IP_CUR_LVL_0P09 98
  132. #define USB_CH_IP_CUR_LVL_0P19 193
  133. #define USB_CH_IP_CUR_LVL_0P29 290
  134. #define USB_CH_IP_CUR_LVL_0P38 380
  135. #define USB_CH_IP_CUR_LVL_0P45 450
  136. #define USB_CH_IP_CUR_LVL_0P5 500
  137. #define USB_CH_IP_CUR_LVL_0P6 600
  138. #define USB_CH_IP_CUR_LVL_0P7 700
  139. #define USB_CH_IP_CUR_LVL_0P8 800
  140. #define USB_CH_IP_CUR_LVL_0P9 900
  141. #define USB_CH_IP_CUR_LVL_1P0 1000
  142. #define USB_CH_IP_CUR_LVL_1P1 1100
  143. #define USB_CH_IP_CUR_LVL_1P3 1300
  144. #define USB_CH_IP_CUR_LVL_1P4 1400
  145. #define USB_CH_IP_CUR_LVL_1P5 1500
  146. #define VBAT_TRESH_IP_CUR_RED 3800
  147. #define to_ab8500_charger_usb_device_info(x) container_of((x), \
  148. struct ab8500_charger, usb_chg)
  149. #define to_ab8500_charger_ac_device_info(x) container_of((x), \
  150. struct ab8500_charger, ac_chg)
  151. /**
  152. * struct ab8500_charger_interrupts - ab8500 interupts
  153. * @name: name of the interrupt
  154. * @isr function pointer to the isr
  155. */
  156. struct ab8500_charger_interrupts {
  157. char *name;
  158. irqreturn_t (*isr)(int irq, void *data);
  159. };
  160. struct ab8500_charger_info {
  161. int charger_connected;
  162. int charger_online;
  163. int charger_voltage;
  164. int cv_active;
  165. bool wd_expired;
  166. int charger_current;
  167. };
  168. struct ab8500_charger_event_flags {
  169. bool mainextchnotok;
  170. bool main_thermal_prot;
  171. bool usb_thermal_prot;
  172. bool vbus_ovv;
  173. bool usbchargernotok;
  174. bool chgwdexp;
  175. bool vbus_collapse;
  176. bool vbus_drop_end;
  177. };
  178. struct ab8500_charger_usb_state {
  179. int usb_current;
  180. int usb_current_tmp;
  181. enum ab8500_usb_state state;
  182. enum ab8500_usb_state state_tmp;
  183. spinlock_t usb_lock;
  184. };
  185. struct ab8500_charger_max_usb_in_curr {
  186. int usb_type_max;
  187. int set_max;
  188. int calculated_max;
  189. };
  190. /**
  191. * struct ab8500_charger - ab8500 Charger device information
  192. * @dev: Pointer to the structure device
  193. * @vbus_detected: VBUS detected
  194. * @vbus_detected_start:
  195. * VBUS detected during startup
  196. * @ac_conn: This will be true when the AC charger has been plugged
  197. * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
  198. * charger is enabled
  199. * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
  200. * charger is enabled
  201. * @vbat Battery voltage
  202. * @old_vbat Previously measured battery voltage
  203. * @usb_device_is_unrecognised USB device is unrecognised by the hardware
  204. * @autopower Indicate if we should have automatic pwron after pwrloss
  205. * @autopower_cfg platform specific power config support for "pwron after pwrloss"
  206. * @invalid_charger_detect_state State when forcing AB to use invalid charger
  207. * @is_aca_rid: Incicate if accessory is ACA type
  208. * @current_stepping_sessions:
  209. * Counter for current stepping sessions
  210. * @parent: Pointer to the struct ab8500
  211. * @adc_main_charger_v ADC channel for main charger voltage
  212. * @adc_main_charger_c ADC channel for main charger current
  213. * @adc_vbus_v ADC channel for USB charger voltage
  214. * @adc_usb_charger_c ADC channel for USB charger current
  215. * @bm: Platform specific battery management information
  216. * @flags: Structure for information about events triggered
  217. * @usb_state: Structure for usb stack information
  218. * @max_usb_in_curr: Max USB charger input current
  219. * @ac_chg: AC charger power supply
  220. * @usb_chg: USB charger power supply
  221. * @ac: Structure that holds the AC charger properties
  222. * @usb: Structure that holds the USB charger properties
  223. * @regu: Pointer to the struct regulator
  224. * @charger_wq: Work queue for the IRQs and checking HW state
  225. * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
  226. * @pm_lock: Lock to prevent system to suspend
  227. * @check_vbat_work Work for checking vbat threshold to adjust vbus current
  228. * @check_hw_failure_work: Work for checking HW state
  229. * @check_usbchgnotok_work: Work for checking USB charger not ok status
  230. * @kick_wd_work: Work for kicking the charger watchdog in case
  231. * of ABB rev 1.* due to the watchog logic bug
  232. * @ac_charger_attached_work: Work for checking if AC charger is still
  233. * connected
  234. * @usb_charger_attached_work: Work for checking if USB charger is still
  235. * connected
  236. * @ac_work: Work for checking AC charger connection
  237. * @detect_usb_type_work: Work for detecting the USB type connected
  238. * @usb_link_status_work: Work for checking the new USB link status
  239. * @usb_state_changed_work: Work for checking USB state
  240. * @attach_work: Work for detecting USB type
  241. * @vbus_drop_end_work: Work for detecting VBUS drop end
  242. * @check_main_thermal_prot_work:
  243. * Work for checking Main thermal status
  244. * @check_usb_thermal_prot_work:
  245. * Work for checking USB thermal status
  246. * @charger_attached_mutex: For controlling the wakelock
  247. */
  248. struct ab8500_charger {
  249. struct device *dev;
  250. bool vbus_detected;
  251. bool vbus_detected_start;
  252. bool ac_conn;
  253. bool vddadc_en_ac;
  254. bool vddadc_en_usb;
  255. int vbat;
  256. int old_vbat;
  257. bool usb_device_is_unrecognised;
  258. bool autopower;
  259. bool autopower_cfg;
  260. int invalid_charger_detect_state;
  261. int is_aca_rid;
  262. atomic_t current_stepping_sessions;
  263. struct ab8500 *parent;
  264. struct iio_channel *adc_main_charger_v;
  265. struct iio_channel *adc_main_charger_c;
  266. struct iio_channel *adc_vbus_v;
  267. struct iio_channel *adc_usb_charger_c;
  268. struct abx500_bm_data *bm;
  269. struct ab8500_charger_event_flags flags;
  270. struct ab8500_charger_usb_state usb_state;
  271. struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
  272. struct ux500_charger ac_chg;
  273. struct ux500_charger usb_chg;
  274. struct ab8500_charger_info ac;
  275. struct ab8500_charger_info usb;
  276. struct regulator *regu;
  277. struct workqueue_struct *charger_wq;
  278. struct mutex usb_ipt_crnt_lock;
  279. struct delayed_work check_vbat_work;
  280. struct delayed_work check_hw_failure_work;
  281. struct delayed_work check_usbchgnotok_work;
  282. struct delayed_work kick_wd_work;
  283. struct delayed_work usb_state_changed_work;
  284. struct delayed_work attach_work;
  285. struct delayed_work ac_charger_attached_work;
  286. struct delayed_work usb_charger_attached_work;
  287. struct delayed_work vbus_drop_end_work;
  288. struct work_struct ac_work;
  289. struct work_struct detect_usb_type_work;
  290. struct work_struct usb_link_status_work;
  291. struct work_struct check_main_thermal_prot_work;
  292. struct work_struct check_usb_thermal_prot_work;
  293. struct usb_phy *usb_phy;
  294. struct notifier_block nb;
  295. struct mutex charger_attached_mutex;
  296. };
  297. /* AC properties */
  298. static enum power_supply_property ab8500_charger_ac_props[] = {
  299. POWER_SUPPLY_PROP_HEALTH,
  300. POWER_SUPPLY_PROP_PRESENT,
  301. POWER_SUPPLY_PROP_ONLINE,
  302. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  303. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  304. POWER_SUPPLY_PROP_CURRENT_NOW,
  305. };
  306. /* USB properties */
  307. static enum power_supply_property ab8500_charger_usb_props[] = {
  308. POWER_SUPPLY_PROP_HEALTH,
  309. POWER_SUPPLY_PROP_CURRENT_AVG,
  310. POWER_SUPPLY_PROP_PRESENT,
  311. POWER_SUPPLY_PROP_ONLINE,
  312. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  313. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  314. POWER_SUPPLY_PROP_CURRENT_NOW,
  315. };
  316. /*
  317. * Function for enabling and disabling sw fallback mode
  318. * should always be disabled when no charger is connected.
  319. */
  320. static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
  321. bool fallback)
  322. {
  323. u8 val;
  324. u8 reg;
  325. u8 bank;
  326. u8 bit;
  327. int ret;
  328. dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
  329. if (is_ab8500(di->parent)) {
  330. bank = 0x15;
  331. reg = 0x0;
  332. bit = 3;
  333. } else {
  334. bank = AB8500_SYS_CTRL1_BLOCK;
  335. reg = AB8500_SW_CONTROL_FALLBACK;
  336. bit = 0;
  337. }
  338. /* read the register containing fallback bit */
  339. ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
  340. if (ret < 0) {
  341. dev_err(di->dev, "%d read failed\n", __LINE__);
  342. return;
  343. }
  344. if (is_ab8500(di->parent)) {
  345. /* enable the OPT emulation registers */
  346. ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
  347. if (ret) {
  348. dev_err(di->dev, "%d write failed\n", __LINE__);
  349. goto disable_otp;
  350. }
  351. }
  352. if (fallback)
  353. val |= (1 << bit);
  354. else
  355. val &= ~(1 << bit);
  356. /* write back the changed fallback bit value to register */
  357. ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
  358. if (ret) {
  359. dev_err(di->dev, "%d write failed\n", __LINE__);
  360. }
  361. disable_otp:
  362. if (is_ab8500(di->parent)) {
  363. /* disable the set OTP registers again */
  364. ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
  365. if (ret) {
  366. dev_err(di->dev, "%d write failed\n", __LINE__);
  367. }
  368. }
  369. }
  370. /**
  371. * ab8500_power_supply_changed - a wrapper with local extensions for
  372. * power_supply_changed
  373. * @di: pointer to the ab8500_charger structure
  374. * @psy: pointer to power_supply_that have changed.
  375. *
  376. */
  377. static void ab8500_power_supply_changed(struct ab8500_charger *di,
  378. struct power_supply *psy)
  379. {
  380. /*
  381. * This happens if we get notifications or interrupts and
  382. * the platform has been configured not to support one or
  383. * other type of charging.
  384. */
  385. if (!psy)
  386. return;
  387. if (di->autopower_cfg) {
  388. if (!di->usb.charger_connected &&
  389. !di->ac.charger_connected &&
  390. di->autopower) {
  391. di->autopower = false;
  392. ab8500_enable_disable_sw_fallback(di, false);
  393. } else if (!di->autopower &&
  394. (di->ac.charger_connected ||
  395. di->usb.charger_connected)) {
  396. di->autopower = true;
  397. ab8500_enable_disable_sw_fallback(di, true);
  398. }
  399. }
  400. power_supply_changed(psy);
  401. }
  402. static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
  403. bool connected)
  404. {
  405. if (connected != di->usb.charger_connected) {
  406. dev_dbg(di->dev, "USB connected:%i\n", connected);
  407. di->usb.charger_connected = connected;
  408. if (!connected)
  409. di->flags.vbus_drop_end = false;
  410. /*
  411. * Sometimes the platform is configured not to support
  412. * USB charging and no psy has been created, but we still
  413. * will get these notifications.
  414. */
  415. if (di->usb_chg.psy) {
  416. sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL,
  417. "present");
  418. }
  419. if (connected) {
  420. mutex_lock(&di->charger_attached_mutex);
  421. mutex_unlock(&di->charger_attached_mutex);
  422. if (is_ab8500(di->parent))
  423. queue_delayed_work(di->charger_wq,
  424. &di->usb_charger_attached_work,
  425. HZ);
  426. } else {
  427. cancel_delayed_work_sync(&di->usb_charger_attached_work);
  428. mutex_lock(&di->charger_attached_mutex);
  429. mutex_unlock(&di->charger_attached_mutex);
  430. }
  431. }
  432. }
  433. /**
  434. * ab8500_charger_get_ac_voltage() - get ac charger voltage
  435. * @di: pointer to the ab8500_charger structure
  436. *
  437. * Returns ac charger voltage (on success)
  438. */
  439. static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
  440. {
  441. int vch, ret;
  442. /* Only measure voltage if the charger is connected */
  443. if (di->ac.charger_connected) {
  444. ret = iio_read_channel_processed(di->adc_main_charger_v, &vch);
  445. if (ret < 0)
  446. dev_err(di->dev, "%s ADC conv failed,\n", __func__);
  447. } else {
  448. vch = 0;
  449. }
  450. return vch;
  451. }
  452. /**
  453. * ab8500_charger_ac_cv() - check if the main charger is in CV mode
  454. * @di: pointer to the ab8500_charger structure
  455. *
  456. * Returns ac charger CV mode (on success) else error code
  457. */
  458. static int ab8500_charger_ac_cv(struct ab8500_charger *di)
  459. {
  460. u8 val;
  461. int ret = 0;
  462. /* Only check CV mode if the charger is online */
  463. if (di->ac.charger_online) {
  464. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  465. AB8500_CH_STATUS1_REG, &val);
  466. if (ret < 0) {
  467. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  468. return 0;
  469. }
  470. if (val & MAIN_CH_CV_ON)
  471. ret = 1;
  472. else
  473. ret = 0;
  474. }
  475. return ret;
  476. }
  477. /**
  478. * ab8500_charger_get_vbus_voltage() - get vbus voltage
  479. * @di: pointer to the ab8500_charger structure
  480. *
  481. * This function returns the vbus voltage.
  482. * Returns vbus voltage (on success)
  483. */
  484. static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
  485. {
  486. int vch, ret;
  487. /* Only measure voltage if the charger is connected */
  488. if (di->usb.charger_connected) {
  489. ret = iio_read_channel_processed(di->adc_vbus_v, &vch);
  490. if (ret < 0)
  491. dev_err(di->dev, "%s ADC conv failed,\n", __func__);
  492. } else {
  493. vch = 0;
  494. }
  495. return vch;
  496. }
  497. /**
  498. * ab8500_charger_get_usb_current() - get usb charger current
  499. * @di: pointer to the ab8500_charger structure
  500. *
  501. * This function returns the usb charger current.
  502. * Returns usb current (on success) and error code on failure
  503. */
  504. static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
  505. {
  506. int ich, ret;
  507. /* Only measure current if the charger is online */
  508. if (di->usb.charger_online) {
  509. ret = iio_read_channel_processed(di->adc_usb_charger_c, &ich);
  510. if (ret < 0)
  511. dev_err(di->dev, "%s ADC conv failed,\n", __func__);
  512. } else {
  513. ich = 0;
  514. }
  515. return ich;
  516. }
  517. /**
  518. * ab8500_charger_get_ac_current() - get ac charger current
  519. * @di: pointer to the ab8500_charger structure
  520. *
  521. * This function returns the ac charger current.
  522. * Returns ac current (on success) and error code on failure.
  523. */
  524. static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
  525. {
  526. int ich, ret;
  527. /* Only measure current if the charger is online */
  528. if (di->ac.charger_online) {
  529. ret = iio_read_channel_processed(di->adc_main_charger_c, &ich);
  530. if (ret < 0)
  531. dev_err(di->dev, "%s ADC conv failed,\n", __func__);
  532. } else {
  533. ich = 0;
  534. }
  535. return ich;
  536. }
  537. /**
  538. * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
  539. * @di: pointer to the ab8500_charger structure
  540. *
  541. * Returns ac charger CV mode (on success) else error code
  542. */
  543. static int ab8500_charger_usb_cv(struct ab8500_charger *di)
  544. {
  545. int ret;
  546. u8 val;
  547. /* Only check CV mode if the charger is online */
  548. if (di->usb.charger_online) {
  549. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  550. AB8500_CH_USBCH_STAT1_REG, &val);
  551. if (ret < 0) {
  552. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  553. return 0;
  554. }
  555. if (val & USB_CH_CV_ON)
  556. ret = 1;
  557. else
  558. ret = 0;
  559. } else {
  560. ret = 0;
  561. }
  562. return ret;
  563. }
  564. /**
  565. * ab8500_charger_detect_chargers() - Detect the connected chargers
  566. * @di: pointer to the ab8500_charger structure
  567. * @probe: if probe, don't delay and wait for HW
  568. *
  569. * Returns the type of charger connected.
  570. * For USB it will not mean we can actually charge from it
  571. * but that there is a USB cable connected that we have to
  572. * identify. This is used during startup when we don't get
  573. * interrupts of the charger detection
  574. *
  575. * Returns an integer value, that means,
  576. * NO_PW_CONN no power supply is connected
  577. * AC_PW_CONN if the AC power supply is connected
  578. * USB_PW_CONN if the USB power supply is connected
  579. * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
  580. */
  581. static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
  582. {
  583. int result = NO_PW_CONN;
  584. int ret;
  585. u8 val;
  586. /* Check for AC charger */
  587. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  588. AB8500_CH_STATUS1_REG, &val);
  589. if (ret < 0) {
  590. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  591. return ret;
  592. }
  593. if (val & MAIN_CH_DET)
  594. result = AC_PW_CONN;
  595. /* Check for USB charger */
  596. if (!probe) {
  597. /*
  598. * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
  599. * when disconnecting ACA even though no
  600. * charger was connected. Try waiting a little
  601. * longer than the 100 ms of VBUS_DET_DBNC100...
  602. */
  603. msleep(110);
  604. }
  605. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  606. AB8500_CH_USBCH_STAT1_REG, &val);
  607. if (ret < 0) {
  608. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  609. return ret;
  610. }
  611. dev_dbg(di->dev,
  612. "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
  613. val);
  614. if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
  615. result |= USB_PW_CONN;
  616. return result;
  617. }
  618. /**
  619. * ab8500_charger_max_usb_curr() - get the max curr for the USB type
  620. * @di: pointer to the ab8500_charger structure
  621. * @link_status: the identified USB type
  622. *
  623. * Get the maximum current that is allowed to be drawn from the host
  624. * based on the USB type.
  625. * Returns error code in case of failure else 0 on success
  626. */
  627. static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
  628. enum ab8500_charger_link_status link_status)
  629. {
  630. int ret = 0;
  631. di->usb_device_is_unrecognised = false;
  632. /*
  633. * Platform only supports USB 2.0.
  634. * This means that charging current from USB source
  635. * is maximum 500 mA. Every occurrence of USB_STAT_*_HOST_*
  636. * should set USB_CH_IP_CUR_LVL_0P5.
  637. */
  638. switch (link_status) {
  639. case USB_STAT_STD_HOST_NC:
  640. case USB_STAT_STD_HOST_C_NS:
  641. case USB_STAT_STD_HOST_C_S:
  642. dev_dbg(di->dev, "USB Type - Standard host is "
  643. "detected through USB driver\n");
  644. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  645. di->is_aca_rid = 0;
  646. break;
  647. case USB_STAT_HOST_CHG_HS_CHIRP:
  648. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  649. di->is_aca_rid = 0;
  650. break;
  651. case USB_STAT_HOST_CHG_HS:
  652. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  653. di->is_aca_rid = 0;
  654. break;
  655. case USB_STAT_ACA_RID_C_HS:
  656. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
  657. di->is_aca_rid = 0;
  658. break;
  659. case USB_STAT_ACA_RID_A:
  660. /*
  661. * Dedicated charger level minus maximum current accessory
  662. * can consume (900mA). Closest level is 500mA
  663. */
  664. dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
  665. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  666. di->is_aca_rid = 1;
  667. break;
  668. case USB_STAT_ACA_RID_B:
  669. /*
  670. * Dedicated charger level minus 120mA (20mA for ACA and
  671. * 100mA for potential accessory). Closest level is 1300mA
  672. */
  673. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
  674. dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
  675. di->max_usb_in_curr.usb_type_max);
  676. di->is_aca_rid = 1;
  677. break;
  678. case USB_STAT_HOST_CHG_NM:
  679. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  680. di->is_aca_rid = 0;
  681. break;
  682. case USB_STAT_DEDICATED_CHG:
  683. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
  684. di->is_aca_rid = 0;
  685. break;
  686. case USB_STAT_ACA_RID_C_HS_CHIRP:
  687. case USB_STAT_ACA_RID_C_NM:
  688. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
  689. di->is_aca_rid = 1;
  690. break;
  691. case USB_STAT_NOT_CONFIGURED:
  692. if (di->vbus_detected) {
  693. di->usb_device_is_unrecognised = true;
  694. dev_dbg(di->dev, "USB Type - Legacy charger.\n");
  695. di->max_usb_in_curr.usb_type_max =
  696. USB_CH_IP_CUR_LVL_1P5;
  697. break;
  698. }
  699. fallthrough;
  700. case USB_STAT_HM_IDGND:
  701. dev_err(di->dev, "USB Type - Charging not allowed\n");
  702. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
  703. ret = -ENXIO;
  704. break;
  705. case USB_STAT_RESERVED:
  706. if (is_ab8500(di->parent)) {
  707. di->flags.vbus_collapse = true;
  708. dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
  709. "VBUS has collapsed\n");
  710. ret = -ENXIO;
  711. break;
  712. } else {
  713. dev_dbg(di->dev, "USB Type - Charging not allowed\n");
  714. di->max_usb_in_curr.usb_type_max =
  715. USB_CH_IP_CUR_LVL_0P05;
  716. dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
  717. link_status,
  718. di->max_usb_in_curr.usb_type_max);
  719. ret = -ENXIO;
  720. break;
  721. }
  722. case USB_STAT_CARKIT_1:
  723. case USB_STAT_CARKIT_2:
  724. case USB_STAT_ACA_DOCK_CHARGER:
  725. case USB_STAT_CHARGER_LINE_1:
  726. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  727. dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
  728. di->max_usb_in_curr.usb_type_max);
  729. break;
  730. case USB_STAT_NOT_VALID_LINK:
  731. dev_err(di->dev, "USB Type invalid - try charging anyway\n");
  732. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  733. break;
  734. default:
  735. dev_err(di->dev, "USB Type - Unknown\n");
  736. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
  737. ret = -ENXIO;
  738. break;
  739. }
  740. di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
  741. dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
  742. link_status, di->max_usb_in_curr.set_max);
  743. return ret;
  744. }
  745. /**
  746. * ab8500_charger_read_usb_type() - read the type of usb connected
  747. * @di: pointer to the ab8500_charger structure
  748. *
  749. * Detect the type of the plugged USB
  750. * Returns error code in case of failure else 0 on success
  751. */
  752. static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
  753. {
  754. int ret;
  755. u8 val;
  756. ret = abx500_get_register_interruptible(di->dev,
  757. AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
  758. if (ret < 0) {
  759. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  760. return ret;
  761. }
  762. if (is_ab8500(di->parent))
  763. ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
  764. AB8500_USB_LINE_STAT_REG, &val);
  765. else
  766. ret = abx500_get_register_interruptible(di->dev,
  767. AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
  768. if (ret < 0) {
  769. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  770. return ret;
  771. }
  772. /* get the USB type */
  773. if (is_ab8500(di->parent))
  774. val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
  775. else
  776. val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
  777. ret = ab8500_charger_max_usb_curr(di,
  778. (enum ab8500_charger_link_status) val);
  779. return ret;
  780. }
  781. /**
  782. * ab8500_charger_detect_usb_type() - get the type of usb connected
  783. * @di: pointer to the ab8500_charger structure
  784. *
  785. * Detect the type of the plugged USB
  786. * Returns error code in case of failure else 0 on success
  787. */
  788. static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
  789. {
  790. int i, ret;
  791. u8 val;
  792. /*
  793. * On getting the VBUS rising edge detect interrupt there
  794. * is a 250ms delay after which the register UsbLineStatus
  795. * is filled with valid data.
  796. */
  797. for (i = 0; i < 10; i++) {
  798. msleep(250);
  799. ret = abx500_get_register_interruptible(di->dev,
  800. AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
  801. &val);
  802. dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
  803. __func__, val);
  804. if (ret < 0) {
  805. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  806. return ret;
  807. }
  808. if (is_ab8500(di->parent))
  809. ret = abx500_get_register_interruptible(di->dev,
  810. AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
  811. else
  812. ret = abx500_get_register_interruptible(di->dev,
  813. AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
  814. if (ret < 0) {
  815. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  816. return ret;
  817. }
  818. dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
  819. val);
  820. /*
  821. * Until the IT source register is read the UsbLineStatus
  822. * register is not updated, hence doing the same
  823. * Revisit this:
  824. */
  825. /* get the USB type */
  826. if (is_ab8500(di->parent))
  827. val = (val & AB8500_USB_LINK_STATUS) >>
  828. USB_LINK_STATUS_SHIFT;
  829. else
  830. val = (val & AB8505_USB_LINK_STATUS) >>
  831. USB_LINK_STATUS_SHIFT;
  832. if (val)
  833. break;
  834. }
  835. ret = ab8500_charger_max_usb_curr(di,
  836. (enum ab8500_charger_link_status) val);
  837. return ret;
  838. }
  839. /*
  840. * This array maps the raw hex value to charger voltage used by the AB8500
  841. * Values taken from the UM0836
  842. */
  843. static int ab8500_charger_voltage_map[] = {
  844. 3500 ,
  845. 3525 ,
  846. 3550 ,
  847. 3575 ,
  848. 3600 ,
  849. 3625 ,
  850. 3650 ,
  851. 3675 ,
  852. 3700 ,
  853. 3725 ,
  854. 3750 ,
  855. 3775 ,
  856. 3800 ,
  857. 3825 ,
  858. 3850 ,
  859. 3875 ,
  860. 3900 ,
  861. 3925 ,
  862. 3950 ,
  863. 3975 ,
  864. 4000 ,
  865. 4025 ,
  866. 4050 ,
  867. 4060 ,
  868. 4070 ,
  869. 4080 ,
  870. 4090 ,
  871. 4100 ,
  872. 4110 ,
  873. 4120 ,
  874. 4130 ,
  875. 4140 ,
  876. 4150 ,
  877. 4160 ,
  878. 4170 ,
  879. 4180 ,
  880. 4190 ,
  881. 4200 ,
  882. 4210 ,
  883. 4220 ,
  884. 4230 ,
  885. 4240 ,
  886. 4250 ,
  887. 4260 ,
  888. 4270 ,
  889. 4280 ,
  890. 4290 ,
  891. 4300 ,
  892. 4310 ,
  893. 4320 ,
  894. 4330 ,
  895. 4340 ,
  896. 4350 ,
  897. 4360 ,
  898. 4370 ,
  899. 4380 ,
  900. 4390 ,
  901. 4400 ,
  902. 4410 ,
  903. 4420 ,
  904. 4430 ,
  905. 4440 ,
  906. 4450 ,
  907. 4460 ,
  908. 4470 ,
  909. 4480 ,
  910. 4490 ,
  911. 4500 ,
  912. 4510 ,
  913. 4520 ,
  914. 4530 ,
  915. 4540 ,
  916. 4550 ,
  917. 4560 ,
  918. 4570 ,
  919. 4580 ,
  920. 4590 ,
  921. 4600 ,
  922. };
  923. static int ab8500_voltage_to_regval(int voltage)
  924. {
  925. int i;
  926. /* Special case for voltage below 3.5V */
  927. if (voltage < ab8500_charger_voltage_map[0])
  928. return LOW_VOLT_REG;
  929. for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
  930. if (voltage < ab8500_charger_voltage_map[i])
  931. return i - 1;
  932. }
  933. /* If not last element, return error */
  934. i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
  935. if (voltage == ab8500_charger_voltage_map[i])
  936. return i;
  937. else
  938. return -1;
  939. }
  940. static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
  941. {
  942. int i;
  943. if (curr < di->bm->chg_output_curr[0])
  944. return 0;
  945. for (i = 0; i < di->bm->n_chg_out_curr; i++) {
  946. if (curr < di->bm->chg_output_curr[i])
  947. return i - 1;
  948. }
  949. /* If not last element, return error */
  950. i = di->bm->n_chg_out_curr - 1;
  951. if (curr == di->bm->chg_output_curr[i])
  952. return i;
  953. else
  954. return -1;
  955. }
  956. static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
  957. {
  958. int i;
  959. if (curr < di->bm->chg_input_curr[0])
  960. return 0;
  961. for (i = 0; i < di->bm->n_chg_in_curr; i++) {
  962. if (curr < di->bm->chg_input_curr[i])
  963. return i - 1;
  964. }
  965. /* If not last element, return error */
  966. i = di->bm->n_chg_in_curr - 1;
  967. if (curr == di->bm->chg_input_curr[i])
  968. return i;
  969. else
  970. return -1;
  971. }
  972. /**
  973. * ab8500_charger_get_usb_cur() - get usb current
  974. * @di: pointer to the ab8500_charger structre
  975. *
  976. * The usb stack provides the maximum current that can be drawn from
  977. * the standard usb host. This will be in mA.
  978. * This function converts current in mA to a value that can be written
  979. * to the register. Returns -1 if charging is not allowed
  980. */
  981. static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
  982. {
  983. int ret = 0;
  984. switch (di->usb_state.usb_current) {
  985. case 100:
  986. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
  987. break;
  988. case 200:
  989. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
  990. break;
  991. case 300:
  992. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
  993. break;
  994. case 400:
  995. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
  996. break;
  997. case 500:
  998. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  999. break;
  1000. default:
  1001. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
  1002. ret = -EPERM;
  1003. break;
  1004. }
  1005. di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
  1006. return ret;
  1007. }
  1008. /**
  1009. * ab8500_charger_check_continue_stepping() - Check to allow stepping
  1010. * @di: pointer to the ab8500_charger structure
  1011. * @reg: select what charger register to check
  1012. *
  1013. * Check if current stepping should be allowed to continue.
  1014. * Checks if charger source has not collapsed. If it has, further stepping
  1015. * is not allowed.
  1016. */
  1017. static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
  1018. int reg)
  1019. {
  1020. if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
  1021. return !di->flags.vbus_drop_end;
  1022. else
  1023. return true;
  1024. }
  1025. /**
  1026. * ab8500_charger_set_current() - set charger current
  1027. * @di: pointer to the ab8500_charger structure
  1028. * @ich: charger current, in mA
  1029. * @reg: select what charger register to set
  1030. *
  1031. * Set charger current.
  1032. * There is no state machine in the AB to step up/down the charger
  1033. * current to avoid dips and spikes on MAIN, VBUS and VBAT when
  1034. * charging is started. Instead we need to implement
  1035. * this charger current step-up/down here.
  1036. * Returns error code in case of failure else 0(on success)
  1037. */
  1038. static int ab8500_charger_set_current(struct ab8500_charger *di,
  1039. int ich, int reg)
  1040. {
  1041. int ret = 0;
  1042. int curr_index, prev_curr_index, shift_value, i;
  1043. u8 reg_value;
  1044. u32 step_udelay;
  1045. bool no_stepping = false;
  1046. atomic_inc(&di->current_stepping_sessions);
  1047. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  1048. reg, &reg_value);
  1049. if (ret < 0) {
  1050. dev_err(di->dev, "%s read failed\n", __func__);
  1051. goto exit_set_current;
  1052. }
  1053. switch (reg) {
  1054. case AB8500_MCH_IPT_CURLVL_REG:
  1055. shift_value = MAIN_CH_INPUT_CURR_SHIFT;
  1056. prev_curr_index = (reg_value >> shift_value);
  1057. curr_index = ab8500_current_to_regval(di, ich);
  1058. step_udelay = STEP_UDELAY;
  1059. if (!di->ac.charger_connected)
  1060. no_stepping = true;
  1061. break;
  1062. case AB8500_USBCH_IPT_CRNTLVL_REG:
  1063. shift_value = VBUS_IN_CURR_LIM_SHIFT;
  1064. prev_curr_index = (reg_value >> shift_value);
  1065. curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
  1066. step_udelay = STEP_UDELAY * 100;
  1067. if (!di->usb.charger_connected)
  1068. no_stepping = true;
  1069. break;
  1070. case AB8500_CH_OPT_CRNTLVL_REG:
  1071. shift_value = 0;
  1072. prev_curr_index = (reg_value >> shift_value);
  1073. curr_index = ab8500_current_to_regval(di, ich);
  1074. step_udelay = STEP_UDELAY;
  1075. if (curr_index && (curr_index - prev_curr_index) > 1)
  1076. step_udelay *= 100;
  1077. if (!di->usb.charger_connected && !di->ac.charger_connected)
  1078. no_stepping = true;
  1079. break;
  1080. default:
  1081. dev_err(di->dev, "%s current register not valid\n", __func__);
  1082. ret = -ENXIO;
  1083. goto exit_set_current;
  1084. }
  1085. if (curr_index < 0) {
  1086. dev_err(di->dev, "requested current limit out-of-range\n");
  1087. ret = -ENXIO;
  1088. goto exit_set_current;
  1089. }
  1090. /* only update current if it's been changed */
  1091. if (prev_curr_index == curr_index) {
  1092. dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
  1093. __func__, reg);
  1094. ret = 0;
  1095. goto exit_set_current;
  1096. }
  1097. dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
  1098. __func__, ich, reg);
  1099. if (no_stepping) {
  1100. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1101. reg, (u8)curr_index << shift_value);
  1102. if (ret)
  1103. dev_err(di->dev, "%s write failed\n", __func__);
  1104. } else if (prev_curr_index > curr_index) {
  1105. for (i = prev_curr_index - 1; i >= curr_index; i--) {
  1106. dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
  1107. (u8) i << shift_value, reg);
  1108. ret = abx500_set_register_interruptible(di->dev,
  1109. AB8500_CHARGER, reg, (u8)i << shift_value);
  1110. if (ret) {
  1111. dev_err(di->dev, "%s write failed\n", __func__);
  1112. goto exit_set_current;
  1113. }
  1114. if (i != curr_index)
  1115. usleep_range(step_udelay, step_udelay * 2);
  1116. }
  1117. } else {
  1118. bool allow = true;
  1119. for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
  1120. dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
  1121. (u8)i << shift_value, reg);
  1122. ret = abx500_set_register_interruptible(di->dev,
  1123. AB8500_CHARGER, reg, (u8)i << shift_value);
  1124. if (ret) {
  1125. dev_err(di->dev, "%s write failed\n", __func__);
  1126. goto exit_set_current;
  1127. }
  1128. if (i != curr_index)
  1129. usleep_range(step_udelay, step_udelay * 2);
  1130. allow = ab8500_charger_check_continue_stepping(di, reg);
  1131. }
  1132. }
  1133. exit_set_current:
  1134. atomic_dec(&di->current_stepping_sessions);
  1135. return ret;
  1136. }
  1137. /**
  1138. * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
  1139. * @di: pointer to the ab8500_charger structure
  1140. * @ich_in: charger input current limit
  1141. *
  1142. * Sets the current that can be drawn from the USB host
  1143. * Returns error code in case of failure else 0(on success)
  1144. */
  1145. static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
  1146. int ich_in)
  1147. {
  1148. int min_value;
  1149. int ret;
  1150. /* We should always use to lowest current limit */
  1151. min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
  1152. if (di->max_usb_in_curr.set_max > 0)
  1153. min_value = min(di->max_usb_in_curr.set_max, min_value);
  1154. if (di->usb_state.usb_current >= 0)
  1155. min_value = min(di->usb_state.usb_current, min_value);
  1156. switch (min_value) {
  1157. case 100:
  1158. if (di->vbat < VBAT_TRESH_IP_CUR_RED)
  1159. min_value = USB_CH_IP_CUR_LVL_0P05;
  1160. break;
  1161. case 500:
  1162. if (di->vbat < VBAT_TRESH_IP_CUR_RED)
  1163. min_value = USB_CH_IP_CUR_LVL_0P45;
  1164. break;
  1165. default:
  1166. break;
  1167. }
  1168. dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
  1169. mutex_lock(&di->usb_ipt_crnt_lock);
  1170. ret = ab8500_charger_set_current(di, min_value,
  1171. AB8500_USBCH_IPT_CRNTLVL_REG);
  1172. mutex_unlock(&di->usb_ipt_crnt_lock);
  1173. return ret;
  1174. }
  1175. /**
  1176. * ab8500_charger_set_main_in_curr() - set main charger input current
  1177. * @di: pointer to the ab8500_charger structure
  1178. * @ich_in: input charger current, in mA
  1179. *
  1180. * Set main charger input current.
  1181. * Returns error code in case of failure else 0(on success)
  1182. */
  1183. static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
  1184. int ich_in)
  1185. {
  1186. return ab8500_charger_set_current(di, ich_in,
  1187. AB8500_MCH_IPT_CURLVL_REG);
  1188. }
  1189. /**
  1190. * ab8500_charger_set_output_curr() - set charger output current
  1191. * @di: pointer to the ab8500_charger structure
  1192. * @ich_out: output charger current, in mA
  1193. *
  1194. * Set charger output current.
  1195. * Returns error code in case of failure else 0(on success)
  1196. */
  1197. static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
  1198. int ich_out)
  1199. {
  1200. return ab8500_charger_set_current(di, ich_out,
  1201. AB8500_CH_OPT_CRNTLVL_REG);
  1202. }
  1203. /**
  1204. * ab8500_charger_led_en() - turn on/off chargign led
  1205. * @di: pointer to the ab8500_charger structure
  1206. * @on: flag to turn on/off the chargign led
  1207. *
  1208. * Power ON/OFF charging LED indication
  1209. * Returns error code in case of failure else 0(on success)
  1210. */
  1211. static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
  1212. {
  1213. int ret;
  1214. if (on) {
  1215. /* Power ON charging LED indicator, set LED current to 5mA */
  1216. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1217. AB8500_LED_INDICATOR_PWM_CTRL,
  1218. (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
  1219. if (ret) {
  1220. dev_err(di->dev, "Power ON LED failed\n");
  1221. return ret;
  1222. }
  1223. /* LED indicator PWM duty cycle 252/256 */
  1224. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1225. AB8500_LED_INDICATOR_PWM_DUTY,
  1226. LED_INDICATOR_PWM_DUTY_252_256);
  1227. if (ret) {
  1228. dev_err(di->dev, "Set LED PWM duty cycle failed\n");
  1229. return ret;
  1230. }
  1231. } else {
  1232. /* Power off charging LED indicator */
  1233. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1234. AB8500_LED_INDICATOR_PWM_CTRL,
  1235. LED_INDICATOR_PWM_DIS);
  1236. if (ret) {
  1237. dev_err(di->dev, "Power-off LED failed\n");
  1238. return ret;
  1239. }
  1240. }
  1241. return ret;
  1242. }
  1243. /**
  1244. * ab8500_charger_ac_en() - enable or disable ac charging
  1245. * @di: pointer to the ab8500_charger structure
  1246. * @enable: enable/disable flag
  1247. * @vset: charging voltage
  1248. * @iset: charging current
  1249. *
  1250. * Enable/Disable AC/Mains charging and turns on/off the charging led
  1251. * respectively.
  1252. **/
  1253. static int ab8500_charger_ac_en(struct ux500_charger *charger,
  1254. int enable, int vset, int iset)
  1255. {
  1256. int ret;
  1257. int volt_index;
  1258. int curr_index;
  1259. int input_curr_index;
  1260. u8 overshoot = 0;
  1261. struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
  1262. if (enable) {
  1263. /* Check if AC is connected */
  1264. if (!di->ac.charger_connected) {
  1265. dev_err(di->dev, "AC charger not connected\n");
  1266. return -ENXIO;
  1267. }
  1268. /* Enable AC charging */
  1269. dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
  1270. /*
  1271. * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
  1272. * will be triggered every time we enable the VDD ADC supply.
  1273. * This will turn off charging for a short while.
  1274. * It can be avoided by having the supply on when
  1275. * there is a charger enabled. Normally the VDD ADC supply
  1276. * is enabled every time a GPADC conversion is triggered.
  1277. * We will force it to be enabled from this driver to have
  1278. * the GPADC module independent of the AB8500 chargers
  1279. */
  1280. if (!di->vddadc_en_ac) {
  1281. ret = regulator_enable(di->regu);
  1282. if (ret)
  1283. dev_warn(di->dev,
  1284. "Failed to enable regulator\n");
  1285. else
  1286. di->vddadc_en_ac = true;
  1287. }
  1288. /* Check if the requested voltage or current is valid */
  1289. volt_index = ab8500_voltage_to_regval(vset);
  1290. curr_index = ab8500_current_to_regval(di, iset);
  1291. input_curr_index = ab8500_current_to_regval(di,
  1292. di->bm->chg_params->ac_curr_max);
  1293. if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
  1294. dev_err(di->dev,
  1295. "Charger voltage or current too high, "
  1296. "charging not started\n");
  1297. return -ENXIO;
  1298. }
  1299. /* ChVoltLevel: maximum battery charging voltage */
  1300. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1301. AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
  1302. if (ret) {
  1303. dev_err(di->dev, "%s write failed\n", __func__);
  1304. return ret;
  1305. }
  1306. /* MainChInputCurr: current that can be drawn from the charger*/
  1307. ret = ab8500_charger_set_main_in_curr(di,
  1308. di->bm->chg_params->ac_curr_max);
  1309. if (ret) {
  1310. dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
  1311. __func__);
  1312. return ret;
  1313. }
  1314. /* ChOutputCurentLevel: protected output current */
  1315. ret = ab8500_charger_set_output_curr(di, iset);
  1316. if (ret) {
  1317. dev_err(di->dev, "%s "
  1318. "Failed to set ChOutputCurentLevel\n",
  1319. __func__);
  1320. return ret;
  1321. }
  1322. /* Check if VBAT overshoot control should be enabled */
  1323. if (!di->bm->enable_overshoot)
  1324. overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
  1325. /* Enable Main Charger */
  1326. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1327. AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
  1328. if (ret) {
  1329. dev_err(di->dev, "%s write failed\n", __func__);
  1330. return ret;
  1331. }
  1332. /* Power on charging LED indication */
  1333. ret = ab8500_charger_led_en(di, true);
  1334. if (ret < 0)
  1335. dev_err(di->dev, "failed to enable LED\n");
  1336. di->ac.charger_online = 1;
  1337. } else {
  1338. /* Disable AC charging */
  1339. if (is_ab8500_1p1_or_earlier(di->parent)) {
  1340. /*
  1341. * For ABB revision 1.0 and 1.1 there is a bug in the
  1342. * watchdog logic. That means we have to continuously
  1343. * kick the charger watchdog even when no charger is
  1344. * connected. This is only valid once the AC charger
  1345. * has been enabled. This is a bug that is not handled
  1346. * by the algorithm and the watchdog have to be kicked
  1347. * by the charger driver when the AC charger
  1348. * is disabled
  1349. */
  1350. if (di->ac_conn) {
  1351. queue_delayed_work(di->charger_wq,
  1352. &di->kick_wd_work,
  1353. round_jiffies(WD_KICK_INTERVAL));
  1354. }
  1355. /*
  1356. * We can't turn off charging completely
  1357. * due to a bug in AB8500 cut1.
  1358. * If we do, charging will not start again.
  1359. * That is why we set the lowest voltage
  1360. * and current possible
  1361. */
  1362. ret = abx500_set_register_interruptible(di->dev,
  1363. AB8500_CHARGER,
  1364. AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
  1365. if (ret) {
  1366. dev_err(di->dev,
  1367. "%s write failed\n", __func__);
  1368. return ret;
  1369. }
  1370. ret = ab8500_charger_set_output_curr(di, 0);
  1371. if (ret) {
  1372. dev_err(di->dev, "%s "
  1373. "Failed to set ChOutputCurentLevel\n",
  1374. __func__);
  1375. return ret;
  1376. }
  1377. } else {
  1378. ret = abx500_set_register_interruptible(di->dev,
  1379. AB8500_CHARGER,
  1380. AB8500_MCH_CTRL1, 0);
  1381. if (ret) {
  1382. dev_err(di->dev,
  1383. "%s write failed\n", __func__);
  1384. return ret;
  1385. }
  1386. }
  1387. ret = ab8500_charger_led_en(di, false);
  1388. if (ret < 0)
  1389. dev_err(di->dev, "failed to disable LED\n");
  1390. di->ac.charger_online = 0;
  1391. di->ac.wd_expired = false;
  1392. /* Disable regulator if enabled */
  1393. if (di->vddadc_en_ac) {
  1394. regulator_disable(di->regu);
  1395. di->vddadc_en_ac = false;
  1396. }
  1397. dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
  1398. }
  1399. ab8500_power_supply_changed(di, di->ac_chg.psy);
  1400. return ret;
  1401. }
  1402. /**
  1403. * ab8500_charger_usb_en() - enable usb charging
  1404. * @di: pointer to the ab8500_charger structure
  1405. * @enable: enable/disable flag
  1406. * @vset: charging voltage
  1407. * @ich_out: charger output current
  1408. *
  1409. * Enable/Disable USB charging and turns on/off the charging led respectively.
  1410. * Returns error code in case of failure else 0(on success)
  1411. */
  1412. static int ab8500_charger_usb_en(struct ux500_charger *charger,
  1413. int enable, int vset, int ich_out)
  1414. {
  1415. int ret;
  1416. int volt_index;
  1417. int curr_index;
  1418. u8 overshoot = 0;
  1419. struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
  1420. if (enable) {
  1421. /* Check if USB is connected */
  1422. if (!di->usb.charger_connected) {
  1423. dev_err(di->dev, "USB charger not connected\n");
  1424. return -ENXIO;
  1425. }
  1426. /*
  1427. * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
  1428. * will be triggered every time we enable the VDD ADC supply.
  1429. * This will turn off charging for a short while.
  1430. * It can be avoided by having the supply on when
  1431. * there is a charger enabled. Normally the VDD ADC supply
  1432. * is enabled every time a GPADC conversion is triggered.
  1433. * We will force it to be enabled from this driver to have
  1434. * the GPADC module independent of the AB8500 chargers
  1435. */
  1436. if (!di->vddadc_en_usb) {
  1437. ret = regulator_enable(di->regu);
  1438. if (ret)
  1439. dev_warn(di->dev,
  1440. "Failed to enable regulator\n");
  1441. else
  1442. di->vddadc_en_usb = true;
  1443. }
  1444. /* Enable USB charging */
  1445. dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
  1446. /* Check if the requested voltage or current is valid */
  1447. volt_index = ab8500_voltage_to_regval(vset);
  1448. curr_index = ab8500_current_to_regval(di, ich_out);
  1449. if (volt_index < 0 || curr_index < 0) {
  1450. dev_err(di->dev,
  1451. "Charger voltage or current too high, "
  1452. "charging not started\n");
  1453. return -ENXIO;
  1454. }
  1455. /*
  1456. * ChVoltLevel: max voltage up to which battery can be
  1457. * charged
  1458. */
  1459. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1460. AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
  1461. if (ret) {
  1462. dev_err(di->dev, "%s write failed\n", __func__);
  1463. return ret;
  1464. }
  1465. /* Check if VBAT overshoot control should be enabled */
  1466. if (!di->bm->enable_overshoot)
  1467. overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
  1468. /* Enable USB Charger */
  1469. dev_dbg(di->dev,
  1470. "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
  1471. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1472. AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
  1473. if (ret) {
  1474. dev_err(di->dev, "%s write failed\n", __func__);
  1475. return ret;
  1476. }
  1477. /* If success power on charging LED indication */
  1478. ret = ab8500_charger_led_en(di, true);
  1479. if (ret < 0)
  1480. dev_err(di->dev, "failed to enable LED\n");
  1481. di->usb.charger_online = 1;
  1482. /* USBChInputCurr: current that can be drawn from the usb */
  1483. ret = ab8500_charger_set_vbus_in_curr(di,
  1484. di->max_usb_in_curr.usb_type_max);
  1485. if (ret) {
  1486. dev_err(di->dev, "setting USBChInputCurr failed\n");
  1487. return ret;
  1488. }
  1489. /* ChOutputCurentLevel: protected output current */
  1490. ret = ab8500_charger_set_output_curr(di, ich_out);
  1491. if (ret) {
  1492. dev_err(di->dev, "%s "
  1493. "Failed to set ChOutputCurentLevel\n",
  1494. __func__);
  1495. return ret;
  1496. }
  1497. queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
  1498. } else {
  1499. /* Disable USB charging */
  1500. dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
  1501. ret = abx500_set_register_interruptible(di->dev,
  1502. AB8500_CHARGER,
  1503. AB8500_USBCH_CTRL1_REG, 0);
  1504. if (ret) {
  1505. dev_err(di->dev,
  1506. "%s write failed\n", __func__);
  1507. return ret;
  1508. }
  1509. ret = ab8500_charger_led_en(di, false);
  1510. if (ret < 0)
  1511. dev_err(di->dev, "failed to disable LED\n");
  1512. /* USBChInputCurr: current that can be drawn from the usb */
  1513. ret = ab8500_charger_set_vbus_in_curr(di, 0);
  1514. if (ret) {
  1515. dev_err(di->dev, "setting USBChInputCurr failed\n");
  1516. return ret;
  1517. }
  1518. /* ChOutputCurentLevel: protected output current */
  1519. ret = ab8500_charger_set_output_curr(di, 0);
  1520. if (ret) {
  1521. dev_err(di->dev, "%s "
  1522. "Failed to reset ChOutputCurentLevel\n",
  1523. __func__);
  1524. return ret;
  1525. }
  1526. di->usb.charger_online = 0;
  1527. di->usb.wd_expired = false;
  1528. /* Disable regulator if enabled */
  1529. if (di->vddadc_en_usb) {
  1530. regulator_disable(di->regu);
  1531. di->vddadc_en_usb = false;
  1532. }
  1533. dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
  1534. /* Cancel any pending Vbat check work */
  1535. cancel_delayed_work(&di->check_vbat_work);
  1536. }
  1537. ab8500_power_supply_changed(di, di->usb_chg.psy);
  1538. return ret;
  1539. }
  1540. static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
  1541. unsigned long event, void *data)
  1542. {
  1543. int ret;
  1544. struct device *dev = data;
  1545. /*Toggle External charger control pin*/
  1546. ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
  1547. AB8500_SYS_CHARGER_CONTROL_REG,
  1548. EXTERNAL_CHARGER_DISABLE_REG_VAL);
  1549. if (ret < 0) {
  1550. dev_err(dev, "write reg failed %d\n", ret);
  1551. goto out;
  1552. }
  1553. ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
  1554. AB8500_SYS_CHARGER_CONTROL_REG,
  1555. EXTERNAL_CHARGER_ENABLE_REG_VAL);
  1556. if (ret < 0)
  1557. dev_err(dev, "Write reg failed %d\n", ret);
  1558. out:
  1559. return ret;
  1560. }
  1561. /**
  1562. * ab8500_charger_usb_check_enable() - enable usb charging
  1563. * @charger: pointer to the ux500_charger structure
  1564. * @vset: charging voltage
  1565. * @iset: charger output current
  1566. *
  1567. * Check if the VBUS charger has been disconnected and reconnected without
  1568. * AB8500 rising an interrupt. Returns 0 on success.
  1569. */
  1570. static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
  1571. int vset, int iset)
  1572. {
  1573. u8 usbch_ctrl1 = 0;
  1574. int ret = 0;
  1575. struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
  1576. if (!di->usb.charger_connected)
  1577. return ret;
  1578. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  1579. AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
  1580. if (ret < 0) {
  1581. dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
  1582. return ret;
  1583. }
  1584. dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
  1585. if (!(usbch_ctrl1 & USB_CH_ENA)) {
  1586. dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
  1587. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1588. AB8500_CHARGER, AB8500_CHARGER_CTRL,
  1589. DROP_COUNT_RESET, DROP_COUNT_RESET);
  1590. if (ret < 0) {
  1591. dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
  1592. return ret;
  1593. }
  1594. ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
  1595. if (ret < 0) {
  1596. dev_err(di->dev, "Failed to enable VBUS charger %d\n",
  1597. __LINE__);
  1598. return ret;
  1599. }
  1600. }
  1601. return ret;
  1602. }
  1603. /**
  1604. * ab8500_charger_ac_check_enable() - enable usb charging
  1605. * @charger: pointer to the ux500_charger structure
  1606. * @vset: charging voltage
  1607. * @iset: charger output current
  1608. *
  1609. * Check if the AC charger has been disconnected and reconnected without
  1610. * AB8500 rising an interrupt. Returns 0 on success.
  1611. */
  1612. static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
  1613. int vset, int iset)
  1614. {
  1615. u8 mainch_ctrl1 = 0;
  1616. int ret = 0;
  1617. struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
  1618. if (!di->ac.charger_connected)
  1619. return ret;
  1620. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  1621. AB8500_MCH_CTRL1, &mainch_ctrl1);
  1622. if (ret < 0) {
  1623. dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
  1624. return ret;
  1625. }
  1626. dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
  1627. if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
  1628. dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
  1629. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1630. AB8500_CHARGER, AB8500_CHARGER_CTRL,
  1631. DROP_COUNT_RESET, DROP_COUNT_RESET);
  1632. if (ret < 0) {
  1633. dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
  1634. return ret;
  1635. }
  1636. ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
  1637. if (ret < 0) {
  1638. dev_err(di->dev, "failed to enable AC charger %d\n",
  1639. __LINE__);
  1640. return ret;
  1641. }
  1642. }
  1643. return ret;
  1644. }
  1645. /**
  1646. * ab8500_charger_watchdog_kick() - kick charger watchdog
  1647. * @di: pointer to the ab8500_charger structure
  1648. *
  1649. * Kick charger watchdog
  1650. * Returns error code in case of failure else 0(on success)
  1651. */
  1652. static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
  1653. {
  1654. int ret;
  1655. struct ab8500_charger *di;
  1656. if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
  1657. di = to_ab8500_charger_ac_device_info(charger);
  1658. else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
  1659. di = to_ab8500_charger_usb_device_info(charger);
  1660. else
  1661. return -ENXIO;
  1662. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1663. AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
  1664. if (ret)
  1665. dev_err(di->dev, "Failed to kick WD!\n");
  1666. return ret;
  1667. }
  1668. /**
  1669. * ab8500_charger_update_charger_current() - update charger current
  1670. * @di: pointer to the ab8500_charger structure
  1671. *
  1672. * Update the charger output current for the specified charger
  1673. * Returns error code in case of failure else 0(on success)
  1674. */
  1675. static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
  1676. int ich_out)
  1677. {
  1678. int ret;
  1679. struct ab8500_charger *di;
  1680. if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
  1681. di = to_ab8500_charger_ac_device_info(charger);
  1682. else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
  1683. di = to_ab8500_charger_usb_device_info(charger);
  1684. else
  1685. return -ENXIO;
  1686. ret = ab8500_charger_set_output_curr(di, ich_out);
  1687. if (ret) {
  1688. dev_err(di->dev, "%s "
  1689. "Failed to set ChOutputCurentLevel\n",
  1690. __func__);
  1691. return ret;
  1692. }
  1693. /* Reset the main and usb drop input current measurement counter */
  1694. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1695. AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
  1696. if (ret) {
  1697. dev_err(di->dev, "%s write failed\n", __func__);
  1698. return ret;
  1699. }
  1700. return ret;
  1701. }
  1702. static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
  1703. {
  1704. struct power_supply *psy;
  1705. struct power_supply *ext = dev_get_drvdata(dev);
  1706. const char **supplicants = (const char **)ext->supplied_to;
  1707. struct ab8500_charger *di;
  1708. union power_supply_propval ret;
  1709. int j;
  1710. struct ux500_charger *usb_chg;
  1711. usb_chg = (struct ux500_charger *)data;
  1712. psy = usb_chg->psy;
  1713. di = to_ab8500_charger_usb_device_info(usb_chg);
  1714. /* For all psy where the driver name appears in any supplied_to */
  1715. j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
  1716. if (j < 0)
  1717. return 0;
  1718. /* Go through all properties for the psy */
  1719. for (j = 0; j < ext->desc->num_properties; j++) {
  1720. enum power_supply_property prop;
  1721. prop = ext->desc->properties[j];
  1722. if (power_supply_get_property(ext, prop, &ret))
  1723. continue;
  1724. switch (prop) {
  1725. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1726. switch (ext->desc->type) {
  1727. case POWER_SUPPLY_TYPE_BATTERY:
  1728. di->vbat = ret.intval / 1000;
  1729. break;
  1730. default:
  1731. break;
  1732. }
  1733. break;
  1734. default:
  1735. break;
  1736. }
  1737. }
  1738. return 0;
  1739. }
  1740. /**
  1741. * ab8500_charger_check_vbat_work() - keep vbus current within spec
  1742. * @work pointer to the work_struct structure
  1743. *
  1744. * Due to a asic bug it is necessary to lower the input current to the vbus
  1745. * charger when charging with at some specific levels. This issue is only valid
  1746. * for below a certain battery voltage. This function makes sure that the
  1747. * the allowed current limit isn't exceeded.
  1748. */
  1749. static void ab8500_charger_check_vbat_work(struct work_struct *work)
  1750. {
  1751. int t = 10;
  1752. struct ab8500_charger *di = container_of(work,
  1753. struct ab8500_charger, check_vbat_work.work);
  1754. class_for_each_device(power_supply_class, NULL,
  1755. di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
  1756. /* First run old_vbat is 0. */
  1757. if (di->old_vbat == 0)
  1758. di->old_vbat = di->vbat;
  1759. if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
  1760. di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
  1761. (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
  1762. di->vbat > VBAT_TRESH_IP_CUR_RED))) {
  1763. dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
  1764. " old: %d\n", di->max_usb_in_curr.usb_type_max,
  1765. di->vbat, di->old_vbat);
  1766. ab8500_charger_set_vbus_in_curr(di,
  1767. di->max_usb_in_curr.usb_type_max);
  1768. power_supply_changed(di->usb_chg.psy);
  1769. }
  1770. di->old_vbat = di->vbat;
  1771. /*
  1772. * No need to check the battery voltage every second when not close to
  1773. * the threshold.
  1774. */
  1775. if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
  1776. (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
  1777. t = 1;
  1778. queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
  1779. }
  1780. /**
  1781. * ab8500_charger_check_hw_failure_work() - check main charger failure
  1782. * @work: pointer to the work_struct structure
  1783. *
  1784. * Work queue function for checking the main charger status
  1785. */
  1786. static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
  1787. {
  1788. int ret;
  1789. u8 reg_value;
  1790. struct ab8500_charger *di = container_of(work,
  1791. struct ab8500_charger, check_hw_failure_work.work);
  1792. /* Check if the status bits for HW failure is still active */
  1793. if (di->flags.mainextchnotok) {
  1794. ret = abx500_get_register_interruptible(di->dev,
  1795. AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
  1796. if (ret < 0) {
  1797. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1798. return;
  1799. }
  1800. if (!(reg_value & MAIN_CH_NOK)) {
  1801. di->flags.mainextchnotok = false;
  1802. ab8500_power_supply_changed(di, di->ac_chg.psy);
  1803. }
  1804. }
  1805. if (di->flags.vbus_ovv) {
  1806. ret = abx500_get_register_interruptible(di->dev,
  1807. AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
  1808. &reg_value);
  1809. if (ret < 0) {
  1810. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1811. return;
  1812. }
  1813. if (!(reg_value & VBUS_OVV_TH)) {
  1814. di->flags.vbus_ovv = false;
  1815. ab8500_power_supply_changed(di, di->usb_chg.psy);
  1816. }
  1817. }
  1818. /* If we still have a failure, schedule a new check */
  1819. if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
  1820. queue_delayed_work(di->charger_wq,
  1821. &di->check_hw_failure_work, round_jiffies(HZ));
  1822. }
  1823. }
  1824. /**
  1825. * ab8500_charger_kick_watchdog_work() - kick the watchdog
  1826. * @work: pointer to the work_struct structure
  1827. *
  1828. * Work queue function for kicking the charger watchdog.
  1829. *
  1830. * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
  1831. * logic. That means we have to continuously kick the charger
  1832. * watchdog even when no charger is connected. This is only
  1833. * valid once the AC charger has been enabled. This is
  1834. * a bug that is not handled by the algorithm and the
  1835. * watchdog have to be kicked by the charger driver
  1836. * when the AC charger is disabled
  1837. */
  1838. static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
  1839. {
  1840. int ret;
  1841. struct ab8500_charger *di = container_of(work,
  1842. struct ab8500_charger, kick_wd_work.work);
  1843. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1844. AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
  1845. if (ret)
  1846. dev_err(di->dev, "Failed to kick WD!\n");
  1847. /* Schedule a new watchdog kick */
  1848. queue_delayed_work(di->charger_wq,
  1849. &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
  1850. }
  1851. /**
  1852. * ab8500_charger_ac_work() - work to get and set main charger status
  1853. * @work: pointer to the work_struct structure
  1854. *
  1855. * Work queue function for checking the main charger status
  1856. */
  1857. static void ab8500_charger_ac_work(struct work_struct *work)
  1858. {
  1859. int ret;
  1860. struct ab8500_charger *di = container_of(work,
  1861. struct ab8500_charger, ac_work);
  1862. /*
  1863. * Since we can't be sure that the events are received
  1864. * synchronously, we have the check if the main charger is
  1865. * connected by reading the status register
  1866. */
  1867. ret = ab8500_charger_detect_chargers(di, false);
  1868. if (ret < 0)
  1869. return;
  1870. if (ret & AC_PW_CONN) {
  1871. di->ac.charger_connected = 1;
  1872. di->ac_conn = true;
  1873. } else {
  1874. di->ac.charger_connected = 0;
  1875. }
  1876. ab8500_power_supply_changed(di, di->ac_chg.psy);
  1877. sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
  1878. }
  1879. static void ab8500_charger_usb_attached_work(struct work_struct *work)
  1880. {
  1881. struct ab8500_charger *di = container_of(work,
  1882. struct ab8500_charger,
  1883. usb_charger_attached_work.work);
  1884. int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
  1885. int ret, i;
  1886. u8 statval;
  1887. for (i = 0; i < 10; i++) {
  1888. ret = abx500_get_register_interruptible(di->dev,
  1889. AB8500_CHARGER,
  1890. AB8500_CH_USBCH_STAT1_REG,
  1891. &statval);
  1892. if (ret < 0) {
  1893. dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
  1894. goto reschedule;
  1895. }
  1896. if ((statval & usbch) != usbch)
  1897. goto reschedule;
  1898. msleep(CHARGER_STATUS_POLL);
  1899. }
  1900. ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
  1901. mutex_lock(&di->charger_attached_mutex);
  1902. mutex_unlock(&di->charger_attached_mutex);
  1903. return;
  1904. reschedule:
  1905. queue_delayed_work(di->charger_wq,
  1906. &di->usb_charger_attached_work,
  1907. HZ);
  1908. }
  1909. static void ab8500_charger_ac_attached_work(struct work_struct *work)
  1910. {
  1911. struct ab8500_charger *di = container_of(work,
  1912. struct ab8500_charger,
  1913. ac_charger_attached_work.work);
  1914. int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
  1915. MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
  1916. int ret, i;
  1917. u8 statval;
  1918. for (i = 0; i < 10; i++) {
  1919. ret = abx500_get_register_interruptible(di->dev,
  1920. AB8500_CHARGER,
  1921. AB8500_CH_STATUS2_REG,
  1922. &statval);
  1923. if (ret < 0) {
  1924. dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
  1925. goto reschedule;
  1926. }
  1927. if ((statval & mainch) != mainch)
  1928. goto reschedule;
  1929. msleep(CHARGER_STATUS_POLL);
  1930. }
  1931. ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
  1932. queue_work(di->charger_wq, &di->ac_work);
  1933. mutex_lock(&di->charger_attached_mutex);
  1934. mutex_unlock(&di->charger_attached_mutex);
  1935. return;
  1936. reschedule:
  1937. queue_delayed_work(di->charger_wq,
  1938. &di->ac_charger_attached_work,
  1939. HZ);
  1940. }
  1941. /**
  1942. * ab8500_charger_detect_usb_type_work() - work to detect USB type
  1943. * @work: Pointer to the work_struct structure
  1944. *
  1945. * Detect the type of USB plugged
  1946. */
  1947. static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
  1948. {
  1949. int ret;
  1950. struct ab8500_charger *di = container_of(work,
  1951. struct ab8500_charger, detect_usb_type_work);
  1952. /*
  1953. * Since we can't be sure that the events are received
  1954. * synchronously, we have the check if is
  1955. * connected by reading the status register
  1956. */
  1957. ret = ab8500_charger_detect_chargers(di, false);
  1958. if (ret < 0)
  1959. return;
  1960. if (!(ret & USB_PW_CONN)) {
  1961. dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
  1962. di->vbus_detected = false;
  1963. ab8500_charger_set_usb_connected(di, false);
  1964. ab8500_power_supply_changed(di, di->usb_chg.psy);
  1965. } else {
  1966. dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
  1967. di->vbus_detected = true;
  1968. if (is_ab8500_1p1_or_earlier(di->parent)) {
  1969. ret = ab8500_charger_detect_usb_type(di);
  1970. if (!ret) {
  1971. ab8500_charger_set_usb_connected(di, true);
  1972. ab8500_power_supply_changed(di,
  1973. di->usb_chg.psy);
  1974. }
  1975. } else {
  1976. /*
  1977. * For ABB cut2.0 and onwards we have an IRQ,
  1978. * USB_LINK_STATUS that will be triggered when the USB
  1979. * link status changes. The exception is USB connected
  1980. * during startup. Then we don't get a
  1981. * USB_LINK_STATUS IRQ
  1982. */
  1983. if (di->vbus_detected_start) {
  1984. di->vbus_detected_start = false;
  1985. ret = ab8500_charger_detect_usb_type(di);
  1986. if (!ret) {
  1987. ab8500_charger_set_usb_connected(di,
  1988. true);
  1989. ab8500_power_supply_changed(di,
  1990. di->usb_chg.psy);
  1991. }
  1992. }
  1993. }
  1994. }
  1995. }
  1996. /**
  1997. * ab8500_charger_usb_link_attach_work() - work to detect USB type
  1998. * @work: pointer to the work_struct structure
  1999. *
  2000. * Detect the type of USB plugged
  2001. */
  2002. static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
  2003. {
  2004. struct ab8500_charger *di =
  2005. container_of(work, struct ab8500_charger, attach_work.work);
  2006. int ret;
  2007. /* Update maximum input current if USB enumeration is not detected */
  2008. if (!di->usb.charger_online) {
  2009. ret = ab8500_charger_set_vbus_in_curr(di,
  2010. di->max_usb_in_curr.usb_type_max);
  2011. if (ret)
  2012. return;
  2013. }
  2014. ab8500_charger_set_usb_connected(di, true);
  2015. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2016. }
  2017. /**
  2018. * ab8500_charger_usb_link_status_work() - work to detect USB type
  2019. * @work: pointer to the work_struct structure
  2020. *
  2021. * Detect the type of USB plugged
  2022. */
  2023. static void ab8500_charger_usb_link_status_work(struct work_struct *work)
  2024. {
  2025. int detected_chargers;
  2026. int ret;
  2027. u8 val;
  2028. u8 link_status;
  2029. struct ab8500_charger *di = container_of(work,
  2030. struct ab8500_charger, usb_link_status_work);
  2031. /*
  2032. * Since we can't be sure that the events are received
  2033. * synchronously, we have the check if is
  2034. * connected by reading the status register
  2035. */
  2036. detected_chargers = ab8500_charger_detect_chargers(di, false);
  2037. if (detected_chargers < 0)
  2038. return;
  2039. /*
  2040. * Some chargers that breaks the USB spec is
  2041. * identified as invalid by AB8500 and it refuse
  2042. * to start the charging process. but by jumping
  2043. * through a few hoops it can be forced to start.
  2044. */
  2045. if (is_ab8500(di->parent))
  2046. ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
  2047. AB8500_USB_LINE_STAT_REG, &val);
  2048. else
  2049. ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
  2050. AB8500_USB_LINK1_STAT_REG, &val);
  2051. if (ret >= 0)
  2052. dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
  2053. else
  2054. dev_dbg(di->dev, "Error reading USB link status\n");
  2055. if (is_ab8500(di->parent))
  2056. link_status = AB8500_USB_LINK_STATUS;
  2057. else
  2058. link_status = AB8505_USB_LINK_STATUS;
  2059. if (detected_chargers & USB_PW_CONN) {
  2060. if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
  2061. USB_STAT_NOT_VALID_LINK &&
  2062. di->invalid_charger_detect_state == 0) {
  2063. dev_dbg(di->dev,
  2064. "Invalid charger detected, state= 0\n");
  2065. /*Enable charger*/
  2066. abx500_mask_and_set_register_interruptible(di->dev,
  2067. AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
  2068. USB_CH_ENA, USB_CH_ENA);
  2069. /*Enable charger detection*/
  2070. abx500_mask_and_set_register_interruptible(di->dev,
  2071. AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
  2072. USB_CH_DET, USB_CH_DET);
  2073. di->invalid_charger_detect_state = 1;
  2074. /*exit and wait for new link status interrupt.*/
  2075. return;
  2076. }
  2077. if (di->invalid_charger_detect_state == 1) {
  2078. dev_dbg(di->dev,
  2079. "Invalid charger detected, state= 1\n");
  2080. /*Stop charger detection*/
  2081. abx500_mask_and_set_register_interruptible(di->dev,
  2082. AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
  2083. USB_CH_DET, 0x00);
  2084. /*Check link status*/
  2085. if (is_ab8500(di->parent))
  2086. ret = abx500_get_register_interruptible(di->dev,
  2087. AB8500_USB, AB8500_USB_LINE_STAT_REG,
  2088. &val);
  2089. else
  2090. ret = abx500_get_register_interruptible(di->dev,
  2091. AB8500_USB, AB8500_USB_LINK1_STAT_REG,
  2092. &val);
  2093. dev_dbg(di->dev, "USB link status= 0x%02x\n",
  2094. (val & link_status) >> USB_LINK_STATUS_SHIFT);
  2095. di->invalid_charger_detect_state = 2;
  2096. }
  2097. } else {
  2098. di->invalid_charger_detect_state = 0;
  2099. }
  2100. if (!(detected_chargers & USB_PW_CONN)) {
  2101. di->vbus_detected = false;
  2102. ab8500_charger_set_usb_connected(di, false);
  2103. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2104. return;
  2105. }
  2106. dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
  2107. di->vbus_detected = true;
  2108. ret = ab8500_charger_read_usb_type(di);
  2109. if (ret) {
  2110. if (ret == -ENXIO) {
  2111. /* No valid charger type detected */
  2112. ab8500_charger_set_usb_connected(di, false);
  2113. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2114. }
  2115. return;
  2116. }
  2117. if (di->usb_device_is_unrecognised) {
  2118. dev_dbg(di->dev,
  2119. "Potential Legacy Charger device. "
  2120. "Delay work for %d msec for USB enum "
  2121. "to finish",
  2122. WAIT_ACA_RID_ENUMERATION);
  2123. queue_delayed_work(di->charger_wq,
  2124. &di->attach_work,
  2125. msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
  2126. } else if (di->is_aca_rid == 1) {
  2127. /* Only wait once */
  2128. di->is_aca_rid++;
  2129. dev_dbg(di->dev,
  2130. "%s Wait %d msec for USB enum to finish",
  2131. __func__, WAIT_ACA_RID_ENUMERATION);
  2132. queue_delayed_work(di->charger_wq,
  2133. &di->attach_work,
  2134. msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
  2135. } else {
  2136. queue_delayed_work(di->charger_wq,
  2137. &di->attach_work,
  2138. 0);
  2139. }
  2140. }
  2141. static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
  2142. {
  2143. int ret;
  2144. unsigned long flags;
  2145. struct ab8500_charger *di = container_of(work,
  2146. struct ab8500_charger, usb_state_changed_work.work);
  2147. if (!di->vbus_detected) {
  2148. dev_dbg(di->dev,
  2149. "%s !di->vbus_detected\n",
  2150. __func__);
  2151. return;
  2152. }
  2153. spin_lock_irqsave(&di->usb_state.usb_lock, flags);
  2154. di->usb_state.state = di->usb_state.state_tmp;
  2155. di->usb_state.usb_current = di->usb_state.usb_current_tmp;
  2156. spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
  2157. dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
  2158. __func__, di->usb_state.state, di->usb_state.usb_current);
  2159. switch (di->usb_state.state) {
  2160. case AB8500_BM_USB_STATE_RESET_HS:
  2161. case AB8500_BM_USB_STATE_RESET_FS:
  2162. case AB8500_BM_USB_STATE_SUSPEND:
  2163. case AB8500_BM_USB_STATE_MAX:
  2164. ab8500_charger_set_usb_connected(di, false);
  2165. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2166. break;
  2167. case AB8500_BM_USB_STATE_RESUME:
  2168. /*
  2169. * when suspend->resume there should be delay
  2170. * of 1sec for enabling charging
  2171. */
  2172. msleep(1000);
  2173. fallthrough;
  2174. case AB8500_BM_USB_STATE_CONFIGURED:
  2175. /*
  2176. * USB is configured, enable charging with the charging
  2177. * input current obtained from USB driver
  2178. */
  2179. if (!ab8500_charger_get_usb_cur(di)) {
  2180. /* Update maximum input current */
  2181. ret = ab8500_charger_set_vbus_in_curr(di,
  2182. di->max_usb_in_curr.usb_type_max);
  2183. if (ret)
  2184. return;
  2185. ab8500_charger_set_usb_connected(di, true);
  2186. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2187. }
  2188. break;
  2189. default:
  2190. break;
  2191. }
  2192. }
  2193. /**
  2194. * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
  2195. * @work: pointer to the work_struct structure
  2196. *
  2197. * Work queue function for checking the USB charger Not OK status
  2198. */
  2199. static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
  2200. {
  2201. int ret;
  2202. u8 reg_value;
  2203. bool prev_status;
  2204. struct ab8500_charger *di = container_of(work,
  2205. struct ab8500_charger, check_usbchgnotok_work.work);
  2206. /* Check if the status bit for usbchargernotok is still active */
  2207. ret = abx500_get_register_interruptible(di->dev,
  2208. AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
  2209. if (ret < 0) {
  2210. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  2211. return;
  2212. }
  2213. prev_status = di->flags.usbchargernotok;
  2214. if (reg_value & VBUS_CH_NOK) {
  2215. di->flags.usbchargernotok = true;
  2216. /* Check again in 1sec */
  2217. queue_delayed_work(di->charger_wq,
  2218. &di->check_usbchgnotok_work, HZ);
  2219. } else {
  2220. di->flags.usbchargernotok = false;
  2221. di->flags.vbus_collapse = false;
  2222. }
  2223. if (prev_status != di->flags.usbchargernotok)
  2224. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2225. }
  2226. /**
  2227. * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
  2228. * @work: pointer to the work_struct structure
  2229. *
  2230. * Work queue function for checking the Main thermal prot status
  2231. */
  2232. static void ab8500_charger_check_main_thermal_prot_work(
  2233. struct work_struct *work)
  2234. {
  2235. int ret;
  2236. u8 reg_value;
  2237. struct ab8500_charger *di = container_of(work,
  2238. struct ab8500_charger, check_main_thermal_prot_work);
  2239. /* Check if the status bit for main_thermal_prot is still active */
  2240. ret = abx500_get_register_interruptible(di->dev,
  2241. AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
  2242. if (ret < 0) {
  2243. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  2244. return;
  2245. }
  2246. if (reg_value & MAIN_CH_TH_PROT)
  2247. di->flags.main_thermal_prot = true;
  2248. else
  2249. di->flags.main_thermal_prot = false;
  2250. ab8500_power_supply_changed(di, di->ac_chg.psy);
  2251. }
  2252. /**
  2253. * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
  2254. * @work: pointer to the work_struct structure
  2255. *
  2256. * Work queue function for checking the USB thermal prot status
  2257. */
  2258. static void ab8500_charger_check_usb_thermal_prot_work(
  2259. struct work_struct *work)
  2260. {
  2261. int ret;
  2262. u8 reg_value;
  2263. struct ab8500_charger *di = container_of(work,
  2264. struct ab8500_charger, check_usb_thermal_prot_work);
  2265. /* Check if the status bit for usb_thermal_prot is still active */
  2266. ret = abx500_get_register_interruptible(di->dev,
  2267. AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
  2268. if (ret < 0) {
  2269. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  2270. return;
  2271. }
  2272. if (reg_value & USB_CH_TH_PROT)
  2273. di->flags.usb_thermal_prot = true;
  2274. else
  2275. di->flags.usb_thermal_prot = false;
  2276. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2277. }
  2278. /**
  2279. * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
  2280. * @irq: interrupt number
  2281. * @_di: pointer to the ab8500_charger structure
  2282. *
  2283. * Returns IRQ status(IRQ_HANDLED)
  2284. */
  2285. static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
  2286. {
  2287. struct ab8500_charger *di = _di;
  2288. dev_dbg(di->dev, "Main charger unplugged\n");
  2289. queue_work(di->charger_wq, &di->ac_work);
  2290. cancel_delayed_work_sync(&di->ac_charger_attached_work);
  2291. mutex_lock(&di->charger_attached_mutex);
  2292. mutex_unlock(&di->charger_attached_mutex);
  2293. return IRQ_HANDLED;
  2294. }
  2295. /**
  2296. * ab8500_charger_mainchplugdet_handler() - main charger plugged
  2297. * @irq: interrupt number
  2298. * @_di: pointer to the ab8500_charger structure
  2299. *
  2300. * Returns IRQ status(IRQ_HANDLED)
  2301. */
  2302. static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
  2303. {
  2304. struct ab8500_charger *di = _di;
  2305. dev_dbg(di->dev, "Main charger plugged\n");
  2306. queue_work(di->charger_wq, &di->ac_work);
  2307. mutex_lock(&di->charger_attached_mutex);
  2308. mutex_unlock(&di->charger_attached_mutex);
  2309. if (is_ab8500(di->parent))
  2310. queue_delayed_work(di->charger_wq,
  2311. &di->ac_charger_attached_work,
  2312. HZ);
  2313. return IRQ_HANDLED;
  2314. }
  2315. /**
  2316. * ab8500_charger_mainextchnotok_handler() - main charger not ok
  2317. * @irq: interrupt number
  2318. * @_di: pointer to the ab8500_charger structure
  2319. *
  2320. * Returns IRQ status(IRQ_HANDLED)
  2321. */
  2322. static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
  2323. {
  2324. struct ab8500_charger *di = _di;
  2325. dev_dbg(di->dev, "Main charger not ok\n");
  2326. di->flags.mainextchnotok = true;
  2327. ab8500_power_supply_changed(di, di->ac_chg.psy);
  2328. /* Schedule a new HW failure check */
  2329. queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
  2330. return IRQ_HANDLED;
  2331. }
  2332. /**
  2333. * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
  2334. * thermal protection threshold
  2335. * @irq: interrupt number
  2336. * @_di: pointer to the ab8500_charger structure
  2337. *
  2338. * Returns IRQ status(IRQ_HANDLED)
  2339. */
  2340. static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
  2341. {
  2342. struct ab8500_charger *di = _di;
  2343. dev_dbg(di->dev,
  2344. "Die temp above Main charger thermal protection threshold\n");
  2345. queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
  2346. return IRQ_HANDLED;
  2347. }
  2348. /**
  2349. * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
  2350. * thermal protection threshold
  2351. * @irq: interrupt number
  2352. * @_di: pointer to the ab8500_charger structure
  2353. *
  2354. * Returns IRQ status(IRQ_HANDLED)
  2355. */
  2356. static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
  2357. {
  2358. struct ab8500_charger *di = _di;
  2359. dev_dbg(di->dev,
  2360. "Die temp ok for Main charger thermal protection threshold\n");
  2361. queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
  2362. return IRQ_HANDLED;
  2363. }
  2364. static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
  2365. {
  2366. struct ab8500_charger *di = container_of(work,
  2367. struct ab8500_charger, vbus_drop_end_work.work);
  2368. int ret, curr;
  2369. u8 reg_value;
  2370. di->flags.vbus_drop_end = false;
  2371. /* Reset the drop counter */
  2372. abx500_set_register_interruptible(di->dev,
  2373. AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
  2374. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  2375. AB8500_CH_USBCH_STAT2_REG, &reg_value);
  2376. if (ret < 0) {
  2377. dev_err(di->dev, "%s read failed\n", __func__);
  2378. return;
  2379. }
  2380. curr = di->bm->chg_input_curr[
  2381. reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
  2382. if (di->max_usb_in_curr.calculated_max != curr) {
  2383. /* USB source is collapsing */
  2384. di->max_usb_in_curr.calculated_max = curr;
  2385. dev_dbg(di->dev,
  2386. "VBUS input current limiting to %d mA\n",
  2387. di->max_usb_in_curr.calculated_max);
  2388. } else {
  2389. /*
  2390. * USB source can not give more than this amount.
  2391. * Taking more will collapse the source.
  2392. */
  2393. di->max_usb_in_curr.set_max =
  2394. di->max_usb_in_curr.calculated_max;
  2395. dev_dbg(di->dev,
  2396. "VBUS input current limited to %d mA\n",
  2397. di->max_usb_in_curr.set_max);
  2398. }
  2399. if (di->usb.charger_connected)
  2400. ab8500_charger_set_vbus_in_curr(di,
  2401. di->max_usb_in_curr.usb_type_max);
  2402. }
  2403. /**
  2404. * ab8500_charger_vbusdetf_handler() - VBUS falling detected
  2405. * @irq: interrupt number
  2406. * @_di: pointer to the ab8500_charger structure
  2407. *
  2408. * Returns IRQ status(IRQ_HANDLED)
  2409. */
  2410. static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
  2411. {
  2412. struct ab8500_charger *di = _di;
  2413. di->vbus_detected = false;
  2414. dev_dbg(di->dev, "VBUS falling detected\n");
  2415. queue_work(di->charger_wq, &di->detect_usb_type_work);
  2416. return IRQ_HANDLED;
  2417. }
  2418. /**
  2419. * ab8500_charger_vbusdetr_handler() - VBUS rising detected
  2420. * @irq: interrupt number
  2421. * @_di: pointer to the ab8500_charger structure
  2422. *
  2423. * Returns IRQ status(IRQ_HANDLED)
  2424. */
  2425. static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
  2426. {
  2427. struct ab8500_charger *di = _di;
  2428. di->vbus_detected = true;
  2429. dev_dbg(di->dev, "VBUS rising detected\n");
  2430. queue_work(di->charger_wq, &di->detect_usb_type_work);
  2431. return IRQ_HANDLED;
  2432. }
  2433. /**
  2434. * ab8500_charger_usblinkstatus_handler() - USB link status has changed
  2435. * @irq: interrupt number
  2436. * @_di: pointer to the ab8500_charger structure
  2437. *
  2438. * Returns IRQ status(IRQ_HANDLED)
  2439. */
  2440. static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
  2441. {
  2442. struct ab8500_charger *di = _di;
  2443. dev_dbg(di->dev, "USB link status changed\n");
  2444. queue_work(di->charger_wq, &di->usb_link_status_work);
  2445. return IRQ_HANDLED;
  2446. }
  2447. /**
  2448. * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
  2449. * thermal protection threshold
  2450. * @irq: interrupt number
  2451. * @_di: pointer to the ab8500_charger structure
  2452. *
  2453. * Returns IRQ status(IRQ_HANDLED)
  2454. */
  2455. static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
  2456. {
  2457. struct ab8500_charger *di = _di;
  2458. dev_dbg(di->dev,
  2459. "Die temp above USB charger thermal protection threshold\n");
  2460. queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
  2461. return IRQ_HANDLED;
  2462. }
  2463. /**
  2464. * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
  2465. * thermal protection threshold
  2466. * @irq: interrupt number
  2467. * @_di: pointer to the ab8500_charger structure
  2468. *
  2469. * Returns IRQ status(IRQ_HANDLED)
  2470. */
  2471. static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
  2472. {
  2473. struct ab8500_charger *di = _di;
  2474. dev_dbg(di->dev,
  2475. "Die temp ok for USB charger thermal protection threshold\n");
  2476. queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
  2477. return IRQ_HANDLED;
  2478. }
  2479. /**
  2480. * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
  2481. * @irq: interrupt number
  2482. * @_di: pointer to the ab8500_charger structure
  2483. *
  2484. * Returns IRQ status(IRQ_HANDLED)
  2485. */
  2486. static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
  2487. {
  2488. struct ab8500_charger *di = _di;
  2489. dev_dbg(di->dev, "Not allowed USB charger detected\n");
  2490. queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
  2491. return IRQ_HANDLED;
  2492. }
  2493. /**
  2494. * ab8500_charger_chwdexp_handler() - Charger watchdog expired
  2495. * @irq: interrupt number
  2496. * @_di: pointer to the ab8500_charger structure
  2497. *
  2498. * Returns IRQ status(IRQ_HANDLED)
  2499. */
  2500. static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
  2501. {
  2502. struct ab8500_charger *di = _di;
  2503. dev_dbg(di->dev, "Charger watchdog expired\n");
  2504. /*
  2505. * The charger that was online when the watchdog expired
  2506. * needs to be restarted for charging to start again
  2507. */
  2508. if (di->ac.charger_online) {
  2509. di->ac.wd_expired = true;
  2510. ab8500_power_supply_changed(di, di->ac_chg.psy);
  2511. }
  2512. if (di->usb.charger_online) {
  2513. di->usb.wd_expired = true;
  2514. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2515. }
  2516. return IRQ_HANDLED;
  2517. }
  2518. /**
  2519. * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
  2520. * @irq: interrupt number
  2521. * @_di: pointer to the ab8500_charger structure
  2522. *
  2523. * Returns IRQ status(IRQ_HANDLED)
  2524. */
  2525. static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
  2526. {
  2527. struct ab8500_charger *di = _di;
  2528. dev_dbg(di->dev, "VBUS charger drop ended\n");
  2529. di->flags.vbus_drop_end = true;
  2530. /*
  2531. * VBUS might have dropped due to bad connection.
  2532. * Schedule a new input limit set to the value SW requests.
  2533. */
  2534. queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
  2535. round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
  2536. return IRQ_HANDLED;
  2537. }
  2538. /**
  2539. * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
  2540. * @irq: interrupt number
  2541. * @_di: pointer to the ab8500_charger structure
  2542. *
  2543. * Returns IRQ status(IRQ_HANDLED)
  2544. */
  2545. static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
  2546. {
  2547. struct ab8500_charger *di = _di;
  2548. dev_dbg(di->dev, "VBUS overvoltage detected\n");
  2549. di->flags.vbus_ovv = true;
  2550. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2551. /* Schedule a new HW failure check */
  2552. queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
  2553. return IRQ_HANDLED;
  2554. }
  2555. /**
  2556. * ab8500_charger_ac_get_property() - get the ac/mains properties
  2557. * @psy: pointer to the power_supply structure
  2558. * @psp: pointer to the power_supply_property structure
  2559. * @val: pointer to the power_supply_propval union
  2560. *
  2561. * This function gets called when an application tries to get the ac/mains
  2562. * properties by reading the sysfs files.
  2563. * AC/Mains properties are online, present and voltage.
  2564. * online: ac/mains charging is in progress or not
  2565. * present: presence of the ac/mains
  2566. * voltage: AC/Mains voltage
  2567. * Returns error code in case of failure else 0(on success)
  2568. */
  2569. static int ab8500_charger_ac_get_property(struct power_supply *psy,
  2570. enum power_supply_property psp,
  2571. union power_supply_propval *val)
  2572. {
  2573. struct ab8500_charger *di;
  2574. int ret;
  2575. di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
  2576. switch (psp) {
  2577. case POWER_SUPPLY_PROP_HEALTH:
  2578. if (di->flags.mainextchnotok)
  2579. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  2580. else if (di->ac.wd_expired || di->usb.wd_expired)
  2581. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  2582. else if (di->flags.main_thermal_prot)
  2583. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  2584. else
  2585. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  2586. break;
  2587. case POWER_SUPPLY_PROP_ONLINE:
  2588. val->intval = di->ac.charger_online;
  2589. break;
  2590. case POWER_SUPPLY_PROP_PRESENT:
  2591. val->intval = di->ac.charger_connected;
  2592. break;
  2593. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  2594. ret = ab8500_charger_get_ac_voltage(di);
  2595. if (ret >= 0)
  2596. di->ac.charger_voltage = ret;
  2597. /* On error, use previous value */
  2598. val->intval = di->ac.charger_voltage * 1000;
  2599. break;
  2600. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  2601. /*
  2602. * This property is used to indicate when CV mode is entered
  2603. * for the AC charger
  2604. */
  2605. di->ac.cv_active = ab8500_charger_ac_cv(di);
  2606. val->intval = di->ac.cv_active;
  2607. break;
  2608. case POWER_SUPPLY_PROP_CURRENT_NOW:
  2609. ret = ab8500_charger_get_ac_current(di);
  2610. if (ret >= 0)
  2611. di->ac.charger_current = ret;
  2612. val->intval = di->ac.charger_current * 1000;
  2613. break;
  2614. default:
  2615. return -EINVAL;
  2616. }
  2617. return 0;
  2618. }
  2619. /**
  2620. * ab8500_charger_usb_get_property() - get the usb properties
  2621. * @psy: pointer to the power_supply structure
  2622. * @psp: pointer to the power_supply_property structure
  2623. * @val: pointer to the power_supply_propval union
  2624. *
  2625. * This function gets called when an application tries to get the usb
  2626. * properties by reading the sysfs files.
  2627. * USB properties are online, present and voltage.
  2628. * online: usb charging is in progress or not
  2629. * present: presence of the usb
  2630. * voltage: vbus voltage
  2631. * Returns error code in case of failure else 0(on success)
  2632. */
  2633. static int ab8500_charger_usb_get_property(struct power_supply *psy,
  2634. enum power_supply_property psp,
  2635. union power_supply_propval *val)
  2636. {
  2637. struct ab8500_charger *di;
  2638. int ret;
  2639. di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
  2640. switch (psp) {
  2641. case POWER_SUPPLY_PROP_HEALTH:
  2642. if (di->flags.usbchargernotok)
  2643. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  2644. else if (di->ac.wd_expired || di->usb.wd_expired)
  2645. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  2646. else if (di->flags.usb_thermal_prot)
  2647. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  2648. else if (di->flags.vbus_ovv)
  2649. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  2650. else
  2651. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  2652. break;
  2653. case POWER_SUPPLY_PROP_ONLINE:
  2654. val->intval = di->usb.charger_online;
  2655. break;
  2656. case POWER_SUPPLY_PROP_PRESENT:
  2657. val->intval = di->usb.charger_connected;
  2658. break;
  2659. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  2660. ret = ab8500_charger_get_vbus_voltage(di);
  2661. if (ret >= 0)
  2662. di->usb.charger_voltage = ret;
  2663. val->intval = di->usb.charger_voltage * 1000;
  2664. break;
  2665. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  2666. /*
  2667. * This property is used to indicate when CV mode is entered
  2668. * for the USB charger
  2669. */
  2670. di->usb.cv_active = ab8500_charger_usb_cv(di);
  2671. val->intval = di->usb.cv_active;
  2672. break;
  2673. case POWER_SUPPLY_PROP_CURRENT_NOW:
  2674. ret = ab8500_charger_get_usb_current(di);
  2675. if (ret >= 0)
  2676. di->usb.charger_current = ret;
  2677. val->intval = di->usb.charger_current * 1000;
  2678. break;
  2679. case POWER_SUPPLY_PROP_CURRENT_AVG:
  2680. /*
  2681. * This property is used to indicate when VBUS has collapsed
  2682. * due to too high output current from the USB charger
  2683. */
  2684. if (di->flags.vbus_collapse)
  2685. val->intval = 1;
  2686. else
  2687. val->intval = 0;
  2688. break;
  2689. default:
  2690. return -EINVAL;
  2691. }
  2692. return 0;
  2693. }
  2694. /**
  2695. * ab8500_charger_init_hw_registers() - Set up charger related registers
  2696. * @di: pointer to the ab8500_charger structure
  2697. *
  2698. * Set up charger OVV, watchdog and maximum voltage registers as well as
  2699. * charging of the backup battery
  2700. */
  2701. static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
  2702. {
  2703. int ret = 0;
  2704. /* Setup maximum charger current and voltage for ABB cut2.0 */
  2705. if (!is_ab8500_1p1_or_earlier(di->parent)) {
  2706. ret = abx500_set_register_interruptible(di->dev,
  2707. AB8500_CHARGER,
  2708. AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
  2709. if (ret) {
  2710. dev_err(di->dev,
  2711. "failed to set CH_VOLT_LVL_MAX_REG\n");
  2712. goto out;
  2713. }
  2714. ret = abx500_set_register_interruptible(di->dev,
  2715. AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
  2716. CH_OP_CUR_LVL_1P6);
  2717. if (ret) {
  2718. dev_err(di->dev,
  2719. "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
  2720. goto out;
  2721. }
  2722. }
  2723. if (is_ab8505_2p0(di->parent))
  2724. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2725. AB8500_CHARGER,
  2726. AB8500_USBCH_CTRL2_REG,
  2727. VBUS_AUTO_IN_CURR_LIM_ENA,
  2728. VBUS_AUTO_IN_CURR_LIM_ENA);
  2729. else
  2730. /*
  2731. * VBUS OVV set to 6.3V and enable automatic current limitation
  2732. */
  2733. ret = abx500_set_register_interruptible(di->dev,
  2734. AB8500_CHARGER,
  2735. AB8500_USBCH_CTRL2_REG,
  2736. VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
  2737. if (ret) {
  2738. dev_err(di->dev,
  2739. "failed to set automatic current limitation\n");
  2740. goto out;
  2741. }
  2742. /* Enable main watchdog in OTP */
  2743. ret = abx500_set_register_interruptible(di->dev,
  2744. AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
  2745. if (ret) {
  2746. dev_err(di->dev, "failed to enable main WD in OTP\n");
  2747. goto out;
  2748. }
  2749. /* Enable main watchdog */
  2750. ret = abx500_set_register_interruptible(di->dev,
  2751. AB8500_SYS_CTRL2_BLOCK,
  2752. AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
  2753. if (ret) {
  2754. dev_err(di->dev, "failed to enable main watchdog\n");
  2755. goto out;
  2756. }
  2757. /*
  2758. * Due to internal synchronisation, Enable and Kick watchdog bits
  2759. * cannot be enabled in a single write.
  2760. * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
  2761. * between writing Enable then Kick bits.
  2762. */
  2763. udelay(63);
  2764. /* Kick main watchdog */
  2765. ret = abx500_set_register_interruptible(di->dev,
  2766. AB8500_SYS_CTRL2_BLOCK,
  2767. AB8500_MAIN_WDOG_CTRL_REG,
  2768. (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
  2769. if (ret) {
  2770. dev_err(di->dev, "failed to kick main watchdog\n");
  2771. goto out;
  2772. }
  2773. /* Disable main watchdog */
  2774. ret = abx500_set_register_interruptible(di->dev,
  2775. AB8500_SYS_CTRL2_BLOCK,
  2776. AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
  2777. if (ret) {
  2778. dev_err(di->dev, "failed to disable main watchdog\n");
  2779. goto out;
  2780. }
  2781. /* Set watchdog timeout */
  2782. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  2783. AB8500_CH_WD_TIMER_REG, WD_TIMER);
  2784. if (ret) {
  2785. dev_err(di->dev, "failed to set charger watchdog timeout\n");
  2786. goto out;
  2787. }
  2788. ret = ab8500_charger_led_en(di, false);
  2789. if (ret < 0) {
  2790. dev_err(di->dev, "failed to disable LED\n");
  2791. goto out;
  2792. }
  2793. ret = abx500_set_register_interruptible(di->dev,
  2794. AB8500_RTC,
  2795. AB8500_RTC_BACKUP_CHG_REG,
  2796. (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
  2797. if (ret) {
  2798. dev_err(di->dev, "failed to setup backup battery charging\n");
  2799. goto out;
  2800. }
  2801. /* Enable backup battery charging */
  2802. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2803. AB8500_RTC, AB8500_RTC_CTRL_REG,
  2804. RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
  2805. if (ret < 0) {
  2806. dev_err(di->dev, "%s mask and set failed\n", __func__);
  2807. goto out;
  2808. }
  2809. out:
  2810. return ret;
  2811. }
  2812. /*
  2813. * ab8500 charger driver interrupts and their respective isr
  2814. */
  2815. static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
  2816. {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
  2817. {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
  2818. {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
  2819. {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
  2820. {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
  2821. {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
  2822. {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
  2823. {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
  2824. {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
  2825. {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
  2826. {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
  2827. {"VBUS_OVV", ab8500_charger_vbusovv_handler},
  2828. {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
  2829. {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
  2830. };
  2831. static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
  2832. unsigned long event, void *power)
  2833. {
  2834. struct ab8500_charger *di =
  2835. container_of(nb, struct ab8500_charger, nb);
  2836. enum ab8500_usb_state bm_usb_state;
  2837. unsigned mA = *((unsigned *)power);
  2838. if (!di)
  2839. return NOTIFY_DONE;
  2840. if (event != USB_EVENT_VBUS) {
  2841. dev_dbg(di->dev, "not a standard host, returning\n");
  2842. return NOTIFY_DONE;
  2843. }
  2844. /* TODO: State is fabricate here. See if charger really needs USB
  2845. * state or if mA is enough
  2846. */
  2847. if ((di->usb_state.usb_current == 2) && (mA > 2))
  2848. bm_usb_state = AB8500_BM_USB_STATE_RESUME;
  2849. else if (mA == 0)
  2850. bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
  2851. else if (mA == 2)
  2852. bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
  2853. else if (mA >= 8) /* 8, 100, 500 */
  2854. bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
  2855. else /* Should never occur */
  2856. bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
  2857. dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
  2858. __func__, bm_usb_state, mA);
  2859. spin_lock(&di->usb_state.usb_lock);
  2860. di->usb_state.state_tmp = bm_usb_state;
  2861. di->usb_state.usb_current_tmp = mA;
  2862. spin_unlock(&di->usb_state.usb_lock);
  2863. /*
  2864. * wait for some time until you get updates from the usb stack
  2865. * and negotiations are completed
  2866. */
  2867. queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
  2868. return NOTIFY_OK;
  2869. }
  2870. #if defined(CONFIG_PM)
  2871. static int ab8500_charger_resume(struct platform_device *pdev)
  2872. {
  2873. int ret;
  2874. struct ab8500_charger *di = platform_get_drvdata(pdev);
  2875. /*
  2876. * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
  2877. * logic. That means we have to continuously kick the charger
  2878. * watchdog even when no charger is connected. This is only
  2879. * valid once the AC charger has been enabled. This is
  2880. * a bug that is not handled by the algorithm and the
  2881. * watchdog have to be kicked by the charger driver
  2882. * when the AC charger is disabled
  2883. */
  2884. if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
  2885. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  2886. AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
  2887. if (ret)
  2888. dev_err(di->dev, "Failed to kick WD!\n");
  2889. /* If not already pending start a new timer */
  2890. queue_delayed_work(di->charger_wq, &di->kick_wd_work,
  2891. round_jiffies(WD_KICK_INTERVAL));
  2892. }
  2893. /* If we still have a HW failure, schedule a new check */
  2894. if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
  2895. queue_delayed_work(di->charger_wq,
  2896. &di->check_hw_failure_work, 0);
  2897. }
  2898. if (di->flags.vbus_drop_end)
  2899. queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
  2900. return 0;
  2901. }
  2902. static int ab8500_charger_suspend(struct platform_device *pdev,
  2903. pm_message_t state)
  2904. {
  2905. struct ab8500_charger *di = platform_get_drvdata(pdev);
  2906. /* Cancel any pending jobs */
  2907. cancel_delayed_work(&di->check_hw_failure_work);
  2908. cancel_delayed_work(&di->vbus_drop_end_work);
  2909. flush_delayed_work(&di->attach_work);
  2910. flush_delayed_work(&di->usb_charger_attached_work);
  2911. flush_delayed_work(&di->ac_charger_attached_work);
  2912. flush_delayed_work(&di->check_usbchgnotok_work);
  2913. flush_delayed_work(&di->check_vbat_work);
  2914. flush_delayed_work(&di->kick_wd_work);
  2915. flush_work(&di->usb_link_status_work);
  2916. flush_work(&di->ac_work);
  2917. flush_work(&di->detect_usb_type_work);
  2918. if (atomic_read(&di->current_stepping_sessions))
  2919. return -EAGAIN;
  2920. return 0;
  2921. }
  2922. #else
  2923. #define ab8500_charger_suspend NULL
  2924. #define ab8500_charger_resume NULL
  2925. #endif
  2926. static struct notifier_block charger_nb = {
  2927. .notifier_call = ab8500_external_charger_prepare,
  2928. };
  2929. static int ab8500_charger_remove(struct platform_device *pdev)
  2930. {
  2931. struct ab8500_charger *di = platform_get_drvdata(pdev);
  2932. int i, irq, ret;
  2933. /* Disable AC charging */
  2934. ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
  2935. /* Disable USB charging */
  2936. ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
  2937. /* Disable interrupts */
  2938. for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
  2939. irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
  2940. free_irq(irq, di);
  2941. }
  2942. /* Backup battery voltage and current disable */
  2943. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2944. AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
  2945. if (ret < 0)
  2946. dev_err(di->dev, "%s mask and set failed\n", __func__);
  2947. usb_unregister_notifier(di->usb_phy, &di->nb);
  2948. usb_put_phy(di->usb_phy);
  2949. /* Delete the work queue */
  2950. destroy_workqueue(di->charger_wq);
  2951. /* Unregister external charger enable notifier */
  2952. if (!di->ac_chg.enabled)
  2953. blocking_notifier_chain_unregister(
  2954. &charger_notifier_list, &charger_nb);
  2955. flush_scheduled_work();
  2956. if (di->usb_chg.enabled)
  2957. power_supply_unregister(di->usb_chg.psy);
  2958. if (di->ac_chg.enabled && !di->ac_chg.external)
  2959. power_supply_unregister(di->ac_chg.psy);
  2960. return 0;
  2961. }
  2962. static char *supply_interface[] = {
  2963. "ab8500_chargalg",
  2964. "ab8500_fg",
  2965. "ab8500_btemp",
  2966. };
  2967. static const struct power_supply_desc ab8500_ac_chg_desc = {
  2968. .name = "ab8500_ac",
  2969. .type = POWER_SUPPLY_TYPE_MAINS,
  2970. .properties = ab8500_charger_ac_props,
  2971. .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
  2972. .get_property = ab8500_charger_ac_get_property,
  2973. };
  2974. static const struct power_supply_desc ab8500_usb_chg_desc = {
  2975. .name = "ab8500_usb",
  2976. .type = POWER_SUPPLY_TYPE_USB,
  2977. .properties = ab8500_charger_usb_props,
  2978. .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
  2979. .get_property = ab8500_charger_usb_get_property,
  2980. };
  2981. static int ab8500_charger_probe(struct platform_device *pdev)
  2982. {
  2983. struct device_node *np = pdev->dev.of_node;
  2984. struct abx500_bm_data *plat = pdev->dev.platform_data;
  2985. struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
  2986. struct ab8500_charger *di;
  2987. int irq, i, charger_status, ret = 0, ch_stat;
  2988. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  2989. if (!di) {
  2990. dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
  2991. return -ENOMEM;
  2992. }
  2993. if (!plat) {
  2994. dev_err(&pdev->dev, "no battery management data supplied\n");
  2995. return -EINVAL;
  2996. }
  2997. di->bm = plat;
  2998. if (np) {
  2999. ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
  3000. if (ret) {
  3001. dev_err(&pdev->dev, "failed to get battery information\n");
  3002. return ret;
  3003. }
  3004. di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
  3005. } else
  3006. di->autopower_cfg = false;
  3007. /* get parent data */
  3008. di->dev = &pdev->dev;
  3009. di->parent = dev_get_drvdata(pdev->dev.parent);
  3010. /* Get ADC channels */
  3011. di->adc_main_charger_v = devm_iio_channel_get(&pdev->dev,
  3012. "main_charger_v");
  3013. if (IS_ERR(di->adc_main_charger_v)) {
  3014. if (PTR_ERR(di->adc_main_charger_v) == -ENODEV)
  3015. return -EPROBE_DEFER;
  3016. dev_err(&pdev->dev, "failed to get ADC main charger voltage\n");
  3017. return PTR_ERR(di->adc_main_charger_v);
  3018. }
  3019. di->adc_main_charger_c = devm_iio_channel_get(&pdev->dev,
  3020. "main_charger_c");
  3021. if (IS_ERR(di->adc_main_charger_c)) {
  3022. if (PTR_ERR(di->adc_main_charger_c) == -ENODEV)
  3023. return -EPROBE_DEFER;
  3024. dev_err(&pdev->dev, "failed to get ADC main charger current\n");
  3025. return PTR_ERR(di->adc_main_charger_c);
  3026. }
  3027. di->adc_vbus_v = devm_iio_channel_get(&pdev->dev, "vbus_v");
  3028. if (IS_ERR(di->adc_vbus_v)) {
  3029. if (PTR_ERR(di->adc_vbus_v) == -ENODEV)
  3030. return -EPROBE_DEFER;
  3031. dev_err(&pdev->dev, "failed to get ADC USB charger voltage\n");
  3032. return PTR_ERR(di->adc_vbus_v);
  3033. }
  3034. di->adc_usb_charger_c = devm_iio_channel_get(&pdev->dev,
  3035. "usb_charger_c");
  3036. if (IS_ERR(di->adc_usb_charger_c)) {
  3037. if (PTR_ERR(di->adc_usb_charger_c) == -ENODEV)
  3038. return -EPROBE_DEFER;
  3039. dev_err(&pdev->dev, "failed to get ADC USB charger current\n");
  3040. return PTR_ERR(di->adc_usb_charger_c);
  3041. }
  3042. /* initialize lock */
  3043. spin_lock_init(&di->usb_state.usb_lock);
  3044. mutex_init(&di->usb_ipt_crnt_lock);
  3045. di->autopower = false;
  3046. di->invalid_charger_detect_state = 0;
  3047. /* AC and USB supply config */
  3048. ac_psy_cfg.supplied_to = supply_interface;
  3049. ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
  3050. ac_psy_cfg.drv_data = &di->ac_chg;
  3051. usb_psy_cfg.supplied_to = supply_interface;
  3052. usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
  3053. usb_psy_cfg.drv_data = &di->usb_chg;
  3054. /* AC supply */
  3055. /* ux500_charger sub-class */
  3056. di->ac_chg.ops.enable = &ab8500_charger_ac_en;
  3057. di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
  3058. di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
  3059. di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
  3060. di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
  3061. ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
  3062. di->ac_chg.max_out_curr =
  3063. di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
  3064. di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
  3065. di->ac_chg.enabled = di->bm->ac_enabled;
  3066. di->ac_chg.external = false;
  3067. /*notifier for external charger enabling*/
  3068. if (!di->ac_chg.enabled)
  3069. blocking_notifier_chain_register(
  3070. &charger_notifier_list, &charger_nb);
  3071. /* USB supply */
  3072. /* ux500_charger sub-class */
  3073. di->usb_chg.ops.enable = &ab8500_charger_usb_en;
  3074. di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
  3075. di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
  3076. di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
  3077. di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
  3078. ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
  3079. di->usb_chg.max_out_curr =
  3080. di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
  3081. di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
  3082. di->usb_chg.enabled = di->bm->usb_enabled;
  3083. di->usb_chg.external = false;
  3084. di->usb_state.usb_current = -1;
  3085. /* Create a work queue for the charger */
  3086. di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
  3087. WQ_MEM_RECLAIM);
  3088. if (di->charger_wq == NULL) {
  3089. dev_err(di->dev, "failed to create work queue\n");
  3090. return -ENOMEM;
  3091. }
  3092. mutex_init(&di->charger_attached_mutex);
  3093. /* Init work for HW failure check */
  3094. INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
  3095. ab8500_charger_check_hw_failure_work);
  3096. INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
  3097. ab8500_charger_check_usbchargernotok_work);
  3098. INIT_DELAYED_WORK(&di->ac_charger_attached_work,
  3099. ab8500_charger_ac_attached_work);
  3100. INIT_DELAYED_WORK(&di->usb_charger_attached_work,
  3101. ab8500_charger_usb_attached_work);
  3102. /*
  3103. * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
  3104. * logic. That means we have to continuously kick the charger
  3105. * watchdog even when no charger is connected. This is only
  3106. * valid once the AC charger has been enabled. This is
  3107. * a bug that is not handled by the algorithm and the
  3108. * watchdog have to be kicked by the charger driver
  3109. * when the AC charger is disabled
  3110. */
  3111. INIT_DEFERRABLE_WORK(&di->kick_wd_work,
  3112. ab8500_charger_kick_watchdog_work);
  3113. INIT_DEFERRABLE_WORK(&di->check_vbat_work,
  3114. ab8500_charger_check_vbat_work);
  3115. INIT_DELAYED_WORK(&di->attach_work,
  3116. ab8500_charger_usb_link_attach_work);
  3117. INIT_DELAYED_WORK(&di->usb_state_changed_work,
  3118. ab8500_charger_usb_state_changed_work);
  3119. INIT_DELAYED_WORK(&di->vbus_drop_end_work,
  3120. ab8500_charger_vbus_drop_end_work);
  3121. /* Init work for charger detection */
  3122. INIT_WORK(&di->usb_link_status_work,
  3123. ab8500_charger_usb_link_status_work);
  3124. INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
  3125. INIT_WORK(&di->detect_usb_type_work,
  3126. ab8500_charger_detect_usb_type_work);
  3127. /* Init work for checking HW status */
  3128. INIT_WORK(&di->check_main_thermal_prot_work,
  3129. ab8500_charger_check_main_thermal_prot_work);
  3130. INIT_WORK(&di->check_usb_thermal_prot_work,
  3131. ab8500_charger_check_usb_thermal_prot_work);
  3132. /*
  3133. * VDD ADC supply needs to be enabled from this driver when there
  3134. * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
  3135. * interrupts during charging
  3136. */
  3137. di->regu = devm_regulator_get(di->dev, "vddadc");
  3138. if (IS_ERR(di->regu)) {
  3139. ret = PTR_ERR(di->regu);
  3140. dev_err(di->dev, "failed to get vddadc regulator\n");
  3141. goto free_charger_wq;
  3142. }
  3143. /* Initialize OVV, and other registers */
  3144. ret = ab8500_charger_init_hw_registers(di);
  3145. if (ret) {
  3146. dev_err(di->dev, "failed to initialize ABB registers\n");
  3147. goto free_charger_wq;
  3148. }
  3149. /* Register AC charger class */
  3150. if (di->ac_chg.enabled) {
  3151. di->ac_chg.psy = power_supply_register(di->dev,
  3152. &ab8500_ac_chg_desc,
  3153. &ac_psy_cfg);
  3154. if (IS_ERR(di->ac_chg.psy)) {
  3155. dev_err(di->dev, "failed to register AC charger\n");
  3156. ret = PTR_ERR(di->ac_chg.psy);
  3157. goto free_charger_wq;
  3158. }
  3159. }
  3160. /* Register USB charger class */
  3161. if (di->usb_chg.enabled) {
  3162. di->usb_chg.psy = power_supply_register(di->dev,
  3163. &ab8500_usb_chg_desc,
  3164. &usb_psy_cfg);
  3165. if (IS_ERR(di->usb_chg.psy)) {
  3166. dev_err(di->dev, "failed to register USB charger\n");
  3167. ret = PTR_ERR(di->usb_chg.psy);
  3168. goto free_ac;
  3169. }
  3170. }
  3171. di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
  3172. if (IS_ERR_OR_NULL(di->usb_phy)) {
  3173. dev_err(di->dev, "failed to get usb transceiver\n");
  3174. ret = -EINVAL;
  3175. goto free_usb;
  3176. }
  3177. di->nb.notifier_call = ab8500_charger_usb_notifier_call;
  3178. ret = usb_register_notifier(di->usb_phy, &di->nb);
  3179. if (ret) {
  3180. dev_err(di->dev, "failed to register usb notifier\n");
  3181. goto put_usb_phy;
  3182. }
  3183. /* Identify the connected charger types during startup */
  3184. charger_status = ab8500_charger_detect_chargers(di, true);
  3185. if (charger_status & AC_PW_CONN) {
  3186. di->ac.charger_connected = 1;
  3187. di->ac_conn = true;
  3188. ab8500_power_supply_changed(di, di->ac_chg.psy);
  3189. sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
  3190. }
  3191. if (charger_status & USB_PW_CONN) {
  3192. di->vbus_detected = true;
  3193. di->vbus_detected_start = true;
  3194. queue_work(di->charger_wq,
  3195. &di->detect_usb_type_work);
  3196. }
  3197. /* Register interrupts */
  3198. for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
  3199. irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
  3200. if (irq < 0) {
  3201. ret = irq;
  3202. goto free_irq;
  3203. }
  3204. ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
  3205. IRQF_SHARED | IRQF_NO_SUSPEND,
  3206. ab8500_charger_irq[i].name, di);
  3207. if (ret != 0) {
  3208. dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
  3209. , ab8500_charger_irq[i].name, irq, ret);
  3210. goto free_irq;
  3211. }
  3212. dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
  3213. ab8500_charger_irq[i].name, irq, ret);
  3214. }
  3215. platform_set_drvdata(pdev, di);
  3216. mutex_lock(&di->charger_attached_mutex);
  3217. ch_stat = ab8500_charger_detect_chargers(di, false);
  3218. if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
  3219. if (is_ab8500(di->parent))
  3220. queue_delayed_work(di->charger_wq,
  3221. &di->ac_charger_attached_work,
  3222. HZ);
  3223. }
  3224. if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
  3225. if (is_ab8500(di->parent))
  3226. queue_delayed_work(di->charger_wq,
  3227. &di->usb_charger_attached_work,
  3228. HZ);
  3229. }
  3230. mutex_unlock(&di->charger_attached_mutex);
  3231. return ret;
  3232. free_irq:
  3233. usb_unregister_notifier(di->usb_phy, &di->nb);
  3234. /* We also have to free all successfully registered irqs */
  3235. for (i = i - 1; i >= 0; i--) {
  3236. irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
  3237. free_irq(irq, di);
  3238. }
  3239. put_usb_phy:
  3240. usb_put_phy(di->usb_phy);
  3241. free_usb:
  3242. if (di->usb_chg.enabled)
  3243. power_supply_unregister(di->usb_chg.psy);
  3244. free_ac:
  3245. if (di->ac_chg.enabled)
  3246. power_supply_unregister(di->ac_chg.psy);
  3247. free_charger_wq:
  3248. destroy_workqueue(di->charger_wq);
  3249. return ret;
  3250. }
  3251. static const struct of_device_id ab8500_charger_match[] = {
  3252. { .compatible = "stericsson,ab8500-charger", },
  3253. { },
  3254. };
  3255. MODULE_DEVICE_TABLE(of, ab8500_charger_match);
  3256. static struct platform_driver ab8500_charger_driver = {
  3257. .probe = ab8500_charger_probe,
  3258. .remove = ab8500_charger_remove,
  3259. .suspend = ab8500_charger_suspend,
  3260. .resume = ab8500_charger_resume,
  3261. .driver = {
  3262. .name = "ab8500-charger",
  3263. .of_match_table = ab8500_charger_match,
  3264. },
  3265. };
  3266. static int __init ab8500_charger_init(void)
  3267. {
  3268. return platform_driver_register(&ab8500_charger_driver);
  3269. }
  3270. static void __exit ab8500_charger_exit(void)
  3271. {
  3272. platform_driver_unregister(&ab8500_charger_driver);
  3273. }
  3274. subsys_initcall_sync(ab8500_charger_init);
  3275. module_exit(ab8500_charger_exit);
  3276. MODULE_LICENSE("GPL v2");
  3277. MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
  3278. MODULE_ALIAS("platform:ab8500-charger");
  3279. MODULE_DESCRIPTION("AB8500 charger management driver");