ab8500_charger.c 74 KB

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