ab8500_charger.c 83 KB

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