ab8500_charger.c 86 KB

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