ab8500_charger.c 103 KB

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