ab8500_charger.c 101 KB

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