ab8500_charger.c 102 KB

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