ab8500_charger.c 102 KB

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