ab8500_charger.c 93 KB

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