ab8500_charger.c 100 KB

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