ab8500_charger.c 77 KB

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