ab8500_charger.c 99 KB

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